0

I'm having an issue trying to set an input field's value when using HTML entities in that they are coming out literally as " rather than ".

Here is the code I am using:

document.getElementById('inputSurname').setAttribute('value', 'test"""');

in which the output is test""" though I want the output to be test""".

It doesn't look like a double-encoding issue since in the source code I am seeing it the same way I have set it here.

I know I could decode the value from its HTML entity format though this is something I want to avoid if possible for security.

Any help would be much appreciated :)

1
  • 2
    You can't have HTML in a form field, only text. It's the same reason why you can't set the value of an input to be bold via the value, only CSS Commented Jul 11, 2016 at 13:57

3 Answers 3

2

Try this:

document.getElementById('inputSurname').value = 'test"""';

Or if you want to keep &quot:

function myFunction() {
    document.getElementById('myText').value = replaceQuot('test&quot&quot&quot');
}
function replaceQuot(string){
    return string.replace('&quot', '""');
}
Sign up to request clarification or add additional context in comments.

2 Comments

The problem is that I am storing the value as ", and because it's user input I don't really want to decode the HTML entities.
I've made a few tweaks since your answer and managed to get things working now. I've took the string I was using and replaced &quot with \" to avoid any issues from user input and this now looks normal in the input field - so I'll go ahead and mark your answer as the best solution for leading me to this :)
1

Or you can use escape characters.

document.getElementById("inputSurname").setAttribute("value", "test\"\"\"\"");

Comments

0

Well you could just write the new value as 'test"""'.

For other characters however, I'm going to refer you to this answer: HTML Entity Decode

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.