HTML and CSS Reference
In-Depth Information
EXPLANATION
3
Now we are going to insert some text into the paragraph with the createTextNode()
method.
4
After creating a reference to the new text, it is appended to the paragraph with the
appendChild() method.
5
The document.getElementById() returns a reference to the first paragraph. This
will be the reference node, or the node in which the second paragraph will be in-
serted.
6
The new paragraph is inserted above the first paragraph (see Figure 15.16).
7
When the page is loaded, this function will be called. It will cause a new para-
graph to be inserted above the paragraph identified below this line, with the id ,
firstp .
Figure 15.16 The DOM insertBefore() method.
15.7.5 Creating Attributes for Nodes
The setAttribute() method creates a new attribute for an element. If an attribute with that
name ( nodeName ) already exists in the element, it is replaced by the new one. See the
section “Cloning Nodes” later in this chapter for more examples.
FORMAT
reference_to_element.setAttribute(attributeName, value);
reference_to_element.setAttribute(attributeName, value, boolean);
// boolean 0 turns off case-sensitivity, 1 is on the default
// Internet Explorer only
EXAMPLE
var headings = document.getElementsByTagName("h1")
headings[0].setAttribute("id", "firsth1")
 
 
Search WWH ::




Custom Search