AvailabilityDOM Level 1 Core Synopsisvoid setAttribute(String name, String value) throws DOMException; Arguments
ThrowsThis method may throw a DOMException with the following code values:
DescriptionThis method sets the specified attribute to the specified value. If no attribute by that name already exists, a new one is created. Note that Element objects that represent the tags of an HTML document also implement the HTMLElement interface and (usually) one of its tag-specific subinterfaces. As a shortcut, these interfaces define properties that correspond to the standard HTML attributes for each tag, and it is usually easier to set an HTML attribute simply by setting the appropriate property. The value argument is a plain string. If you are working with an XML document and need to include an entity reference in an attribute value, use setAttributeNode( ). Example// Set the TARGET attribute of all links in a document var links = document.body.getElementsByTagName("A"); for(var i = 0; i < links.length; i++) { links[i].setAttribute("TARGET", "newwindow"); } See AlsoElement.getAttribute( ), Element.removeAttribute( ), Element.setAttributeNode( ) |