When to use setAttribute vs .attribute= in JavaScript?

From Javascript: The Definitive Guide, it clarifies things. It notes that HTMLElement objects of a HTML doc define JS properties that correspond to all standard HTML attributes.

So you only need to use setAttribute for non-standard attributes.

Example:

node.className="test"; // works
node.frameborder="0"; // doesn't work - non standard attribute
node.setAttribute('frameborder', '0'); // works

Leave a Comment