ZVON > References > DOM2 Reference

setAttribute (method )

Owning interface and usage:  
Element.setAttribute(name, value)

Member of these other interfaces:  
none

Description:  
Adds a new attribute.

Parameters:  
DOMString name  -  The name of the attribute to create or alter.
DOMString value  -  Value to set in string form.

Returns:  
nothing

Exceptions:  
DOMException INVALID_CHARACTER_ERR
Raised if the specified name contains an invalid character.
DOMException NO_MODIFICATION_ALLOWED_ERR
Raised if this node is readonly.

Note:  
If an attribute with that name is already present in the element, its value is changed to be that of the value parameter. This value is a simple string, it is not parsed as it is being set. So any markup (such as syntax to be recognized as an entity reference) is treated as literal text, and needs to be appropriately escaped by the implementation when it is written out. In order to assign an attribute value that contains entity references, the user must create an Attr node plus any Text and EntityReference nodes, build the appropriate subtree, and use setAttributeNode to assign it as the value of an attribute.


Example:
Text in the first DIV.
Some text in the second DIV.
Some text and element in the third DIV.
We can try another elements. It will be much more interesting.
Text in the last DIV.
Source:
   <div id="doc">
     <div>
       Text in the first DIV.
     </div>
     <div id="DDD" class="secondClass">
       Some text in the second DIV.
     </div>
     <div class="thirdClass">
       Some text and <span id="SSS">element</span> in the third DIV.
     </div>
     <div class="fourthClass">
       We can try <i>another elements</i>.
       It will be much more <b>interesting</b>.
     </div>
     <div>
       Text in the last DIV.
     </div>
   </div>
     
JavaScript:
  var main = document.getElementById('doc');
  main.setAttribute('attr', 'temp');
  var output = main.getAttribute('attr');
Output:
desired your browser
temp