I’ve been creating elements in a different way and I’m not sure the best approach. What is the difference between:
var myselect = document.createElement("select");
myselect.name="blah";
and
var myselect = document.createElement("select");
myselect.setAttribute("name", "blah");
I just recently got tripped up by using:
var label = document.createElement("label");
label.for= "blah";
Which doesn’t work and I had to use setAttribute. Are there advantages/disadvantages of one versus another?
Generally you want to deal with properties in JavaScript instead of attributes.
Properties are on DOM nodes. Attributes are in HTML markup. Sometimes attributes automatically translate to properties.
With respect to
for, usehtmlForinstead.