0

m getting error in the below line of code:

var input2 = document.createElement('<input name=\'password\' type=\'password\' id=\'password\' onblur=\'checkCopy(this)\' onkeyup=\'return handleEnterSubmission(this.form,event)\'  maxlength=\'16\' autocomplete=\'off\' onfocusin=\'checkValue(this.value, this.id);setMarginInIE();\' />');

It works fine in IE8 but giving problem in IE9. Please tell me what's wrong with this piece of code?

Tomalak
  • 332,285
  • 67
  • 532
  • 628
Varun
  • 1
  • 1
  • 1
  • I am getting error in this piece of code :- var input2 = document.createElement(''); Please tell me what is wrong with this piece of code. – Varun Jul 26 '12 at 16:54
  • Same question here: http://stackoverflow.com/questions/5344029/invalid-character-dom-exception-in-ie9 – lamplet Oct 11 '13 at 02:31

1 Answers1

2

The main thing that's wrong with your code is that you apparently never read the documentation of document.createElement() (for example here).

It says (emphasis mine):

var element = document.createElement(tagName);
  • element is the created element object.
  • tagName is a string that specifies the type of element to be created. The nodeName of the created element is initialized with the value of tagName.

You cannot pass full HTML strings to createElement. You can pass a tag name:

var input2 = document.createElement('INPUT');

To create a full block of HTML, use this:

function createElementFromHTML(html) {
  var temp = document.createElement('DIV');
  temp.innerHTML = html;
  return temp.childNodes[0];
}
Tomalak
  • 332,285
  • 67
  • 532
  • 628