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";
One case I found where
setAttribute
is necessary is when changing ARIA attributes, since there are no corresponding properties. For exampleThere's no
x.arialabel
or anything like that, so you have to use setAttribute.Edit: x["aria-label"] does not work. You really do need setAttribute.