Has a best-practice around using setAttribute instead of the dot (.) attribute notation been developed?
E.g.:
myObj.setAttribute("className", "nameOfClass");
myObj.setAttribute("id", "someID");
or
myObj.className = "nameOfClass";
myObj.id = "someID";
You should always use the direct
.attributeform (but see the quirksmode link below) if you want programmatic access in JavaScript. It should handle the different types of attributes (think “onload”) correctly.Use
getAttribute/setAttributewhen you wish to deal with the DOM as it is (e.g. literal text only). Different browsers confuse the two. See Quirks modes: attribute (in)compatibility.