Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting "InvalidCharacterError: String contains an invalid character" for an option in list box

Tags:

javascript

jsp

I have the following line in my jsp file:

var option = document.createElement('<option value="NO">');     

Not sure why this gives me InvalidCharacterError.

Any alternatives?

like image 741
user1776130 Avatar asked Oct 17 '25 17:10

user1776130


2 Answers

You can use this code to add to your tag.

var myoption = document.createElement("option");
myoption.setAttribute("value", "carvalue");
var text = document.createTextNode("maruti");
myoption.appendChild(text);
document.getElementById("mySelect").appendChild(myoption);
like image 111
Gursharan Singh Avatar answered Oct 20 '25 09:10

Gursharan Singh


There was a change in DOM implementation. Before, you could create a new element with the code inside, for instance:

BAD

var anElement = document.createElement("<A HREF=\"http://stackoverflow.com \">StackOverflow</A>")

This implementation now, returns this ‘InvalidCharacterError’. What you must do now is to create the element and to populate it using the innerHTML attribute or the different specific attributes for any node.

GOOD

var anElement = document.createElement("a")
anElement.innerHTML = ("<A HREF=\"http://stackoverflow.com \">StackOverflow</A>")
like image 26
pocjoc Avatar answered Oct 20 '25 07:10

pocjoc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!