views:

49

answers:

1

Hello!

Is there a best-practice indication about using setAttribute instead of the dot (.) attribute notation?

e.g.
myObj.setAttribute("class","nameOfClass");
     - and -
myObj.className="nameOfClass";

OR

myObj.setAttribute("id","someID");
     - and -
myObj.id="someID";


etc

Thank you.

+7  A: 

You should always use the direct ".attribute" form (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 the getAttribute/setAttribute when 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.

pst
YES. Big fat plus one.
Tim Down
And another big fat one. Thank you, pst!
Francisc