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?

2
  • I am getting error in this piece 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();\' />'); Please tell me what is wrong with this piece of code. Commented Jul 26, 2012 at 16:54
  • Same question here: stackoverflow.com/questions/5344029/… Commented Oct 11, 2013 at 2:31

1 Answer 1

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];
}
Sign up to request clarification or add additional context in comments.

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.