0

I am doing something like this:

 var MESSAGE = "part1 part2";
  var fName = $("<input type=\"text\" class=\"fieldname\" size=35  disabled=disabled   value=" + MESSAGE + " />" );

the value of the textbox turns out to be "part1" and is missing part2. i.e the characters after the whitespace . What is the mistake here?

2 Answers 2

5

You should add " to your value:

var MESSAGE = "part1 part2";
  var fName = $("<input type=\"text\" class=\"fieldname\" size=35  disabled=disabled   value=\"" + MESSAGE + "\" />" );

Otherwise your browser interprets "part2" to be a new attribute like "class" or "size".

Sign up to request clarification or add additional context in comments.

Comments

1

One improvement could be to use the jQuery .attr() function, since it automatically escapes HTML entities such as ". It is recommended to do so, unless you are absolutely sure that your message doesn't contain any ",<or >.

var MESSAGE = "part1 part2";
var fName = $("<input">).attr({
    type     : "text",
    class    : "fieldname",
    size     : "35",
    disabled : "disabled",
    value    : MESSAGE
});

And now you are safe.

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.