SetAttribute (): Method adds a new property value to the given element node or changes its existing property value:
Element.setattribute (Attriibutename,attributevalue);
The name and value of the property must be passed to this method as a string, and if the property already exists, its value will be flushed and if it does not exist, the SetAttribute () method can only be used on the attribute node.
In the following example, the SetAttribute () method adds a value of the title property of "This is important" to the ID attribute value that is on the fineprint element:
var message = document.getElementById ("FinePrint"); Message.setattribute ("title", "This is Importanr ");
Regardless of whether the element "FinePrint" has previously had a title attribute, he will now have a value of the title property of this is important.
Even if an element has not been inserted into the document tree, the SetAttribute () method can also be set on its attribute node, and if a new element is created with the createelement () method, you can set it to its properties before adding it to the document tree:
var para = document.createelement ("P");p Ara.setattribute ("id", "fineprint"); var container = document.getElementById ("content"); Container.appendchild (para);
The DOM also provides a getattribute () method that works just the opposite of the setattribute () method, which can be used to retrieve a property value.
Set attribute node (SetAttribute ())