This is a continuation of my study guide for Microsoft Exam 70-480, working toward the Microsoft Certified Solutions Developer (MCSD): Web Applications certification. We are now tackling the Write code that interacts with UI controls objective, specifically DOM modification.
Implement and Manipulate Document Structures and Objects (24%)
Write code that interacts with UI controls
Programmatically add and modify HTML elements
In general, the HTML DOM API or jQuery will be used to manipulate the page structure. It is important to understand the underlying native APIs - jQuery makes our code concise and cross-browser compatible, but a knowledge of what is happening under the covers will help you write more efficient jQuery. Moreover, there are times when you can accomplish something simple with the native API without the need to pull down jQuery.
In the following samples, I will give the native implementation, followed by its jQuery equivalent.
The
document
object is a node that represents the entire HTML document (including the head). Everything in the DOM is a node. It’s important to differentiate between nodes and elements: all elements are nodes, but not all nodes are elements. For example, adiv
element may have anid
attribute - the attribute is a node, but not an element.To get a single element by its ID:
1 2 |
|
- To get an array of elements by tag name:
1 2 |
|
- To get an array of elements by name (most useful for inputs):
1 2 |
|
- To get an array of elements by CSS class:
1 2 |
|
- To traverse between nodes, once you have a node reference:
1 2 3 4 5 6 7 8 9 10 |
|
Important properties of a DOM node:
nodeType
: Integer (enum) representing the type of node. 1 = Element, 2 = Attribute, 3 = Text, 8 = Comment, 9 = DocumentnodeName
: Name of the node. For tag elements, the tag name. For text content, #text.nodeValue
: Value of the node. For elements, null. For text, the text itself, and for attributes, the attribute value.childNodes
: Collection of childrenparentNode
: ParentfirstChild
,lastChild
,nextSibling
,previousSibling
: self-explanatory- jQuery equivalents:
1 2 3 4 5 |
|
Element nodes may have one or more attributes. To access them, the
attributes
member is used. Attributes are not considered children of a node.Access all attributes of an element:
1 2 3 4 |
|
- Get and set the value of a particular attribute:
1 2 3 4 5 6 7 8 9 10 |
|
- Check if an attribute exists:
1 2 |
|
- Remove attribute:
1 2 3 4 |
|
Now we have covered accessing and inspecting page elements via the DOM API and jQuery - it’s time to delve into modifying the structure of the page: adding, removing, and changing the contents of elements.
Once you have a node reference, you can change its value. Tag nodes do not usually have values, but their first child will usually be a text node with value equal to the text content.
To change a tag’s inner text:
1 2 3 4 5 |
|
- To add an element to the DOM tree, you need to create an element, set any required attributes, and then append it to as the last child of an existing node:
1 2 3 4 5 6 |
|
- Adding additional inner text to a span, div, or p tag is accomplished as follows. Note that jQuery
append
is equally effective for HTML or text:
1 2 3 4 |
|
- You can also insert an element as a sibling before an existing node:
1 2 3 4 5 6 7 |
|
Take care to note the differences between
appendChild
andinsertBefore
. A node added withappendChild
will be the last node contained by the target. CallinginsertBefore
has the signature container.insertBefore(nodeToInsert, nodeThatWillBeAfter) - in this case as well, nodeToInsert will be a child of the target, but it will be the prior sibling to nodeThatWillBeAfter.To remove an element from the DOM:
1 2 3 4 |
|
- To replace an existing element:
1 2 3 4 5 6 7 |
|
In both the DOM and jQuery, you can move an element by getting a reference to it and then calling one of the above insertion/replacement methods. A node cannot exist at two places in the DOM, so it will automatically be removed from its previous location.
jQuery in total provides
after
,before
,append
,prepend
,replaceWith
,wrap
,wrapInner
, andwrapAll
for DOM manipulation - these methods are called on the selector for the existing element(s) with the new content as an argument. Companion methodsinsertAfter
,insertBefore
,appendTo
,prependTo
, andreplaceAll
are targeted to the new content with the target selector as an argument. The odd man out isremove
, which is targeted to a selector for content to be removed.
‘Til Next Time
That will conclude DOM manipulation. We’ll pick up next time with media, canvas, and SVG.