Skip Navigation LinksHome > Blog > 2008 > March > 14 > Javascript "setAttribute()"

Javascript "setAttribute()"

I just found out how to modify DOM elements using:

element.setAttribute(name, value);

Adds a new attribute or changes the value of an existing attribute on the specified element. 

  • name is the name of the attribute as a string.
  • value is the desired new value of the attribute.

 Example:

var d = document.getElementById("d1"); 
d.setAttribute("align", "center");
If the specified attribute already exists, then the value of that attribute is changed to the value passed to this function. If it does
not exist, then the attribute is created.