user1736333
user1736333

Reputation: 183

characters after whitespace missing - javascript

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?

Upvotes: 0

Views: 195

Answers (2)

Romain Paulus
Romain Paulus

Reputation: 2368

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.

Upvotes: 1

Tobias Baumeister
Tobias Baumeister

Reputation: 2147

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".

Upvotes: 5

Related Questions