TrevorGoodchild TrevorGoodchild - 1 year ago 40
Javascript Question

JavaScript InvalidCharacterError when scripting out a text box

Working on some pretty old code and really stuck on an odd bug. I have a routine that is writing out some controls and I'm getting an InvalidCharacterError thrown from IE.

Here's the code:

var newFileTextControl = "<input type='Text' name='Q" + answer + "P" + partValueCount + "' value='' style='width:80px' id='Q" + answer + "P" + partValueCount + "' />";

and here is what's being written to the browser:

<input type='Text' name='Q22791P1' value='' style='width:80px' id='Q22791P1' />

Here is where I get the error:

var newFileTextDiv = document.createElement(newFileTextControl);

The error doesn't specify what character is causing the problem, hoping someone can see what I can't here. Thanks in advance.


Creating an element that way is kind of an 'old IE trick'...
It appears that IE9 is more strictly following standards.

To be more 'standards-compliant' I would do:

var input = document.createElement("input");
input.setAttribute("type", "text");

See also this Q/A...

Hope it helps...