In javascript, we can create a new DOM element in the following ways:
Using the createAttribute () + setAttributeNode () dom methods:
var input = document.createElement("input"), type = document.createAttribute("type"); type.nodeValue = "text"; input.setAttributeNode(type); container.appendChild(input);
or just setting attributes directly:
var input = document.createElement("input"); input.type = "text"; container.appendChild(input);
The latter may turn out to be much less code, even if there are only a couple of attributes per element.
Question: Does anyone encounter any shortcomings of the latter method (setting attributes directly)?
I tested this on several browsers (the latest FF, IE, Safari, Opera, old IE - even IE6 worked) and on the basic test (inserting text input with the type, name and attributes of maxLength) that they all passed. Here is the violin , if anyone needs it.
Donamite
source share