π Traversing the DOM with jQuery
Document Object Model (DOM)
There is an important concept in JavaScript that defines the way information is accessed in the web page. The document object contains all the information related to the display of the web page. All the elements are organized by relationship to each other in a tree-like structure. This is called the Document Object Model. It is a way of visualizing all the elements of the web page making access to those items more intuitive. There is a good explanation of the DOM at the W3 Schools. I've also included an example of using many of these methods in the Traversing the DOM Example page.
- Elements nested inside of another element are children of that element.
- All children of an element are siblings.
- Elements that contain another element are the parent of that element.
Code | Explanation |
---|---|
|
For the example code on the left, here are some of the relationships:
Β |
DOM Traversing functions
For example purposes, the following code is used to help demonstrate how each of these functions work.
<div id="content">
<div id="div1">
<p>Some text</p>
<p>More text</p>
</div>
<div id="div2">
<div id="nav">
<p><a href="index.html">Home</a> | <a href="about.html">About Us</a></p>
</div>
<div id="gallery">
<p><img src="x"> <img src="y"> <img src"img3.jpg"></p>
</div>
</div>
</div>
Selector | Explanation |
---|---|
.find(selector) |
Select elements within the current selection.
|
.children(selector) |
Selects only the immediate children of the current selection (not children of children).
|
.parent() |
Locates the parent of the current element.
|
.closest(selector ) |
Locates the closest ancestor of the type selector.
|
.siblings(selector ) |
Selects all of the other elements at the same level of the DOM as the current element. Selector is optional. If used, it will only select siblings of the specified type.
|
.next(selector) .nextAll() |
Selects the next element at the same level as the current element. Selector is optional. If used, it will only select siblings of the specified type.
|
.prev(selector) .prevAll() |
Selects the previous element at the same level as the current element. Selector is optional. If used, it will only select siblings of the specified type.
|
.end( ) |
Rewinds to the prior selection for nested selections.
|
More HTML functions
wrap()
Wraps the contents of the selection in an html tag: $('selector').wrap('<htmlTag></htmlTag>');
Before |
|
Example code |
|
After |
|
unwrap()
Removes the parent tag from around a selection: $('selector').unwrap();
Before |
|
Example code |
|
After |
|
wrapInner()
Wraps the contents of each element in the selection in an html tag: $('selector').wrapinner('<htmlTag></htmlTag>');
Before |
|
Example code |
|
After |
|
empty()
Removes all contents of the selector, but leaves the selector: $('selector').empty();
Before |
|
Example code |
|
After |
|
Delegating Events
Delegating is used to add events to elements added to a page later. There are 2 methods.
on() (preferred method)
To delegate an event to items added after page load, add a second argument to the function which is a selector.
- containerSelector refers to the parent of the selectors that will be bound to the event. This could be an id (#main), element (nav), or even the body element if there is no other parent element.
- event is the type of event that will be bound to the selected nodes
- selector is the selectors that will be bound to the event
- objectliteral is the data to pass to the function
- function can be an anonymous or named function.
$('containerSelector').on('event', 'selector', objectliteral, function() {
//code to respond to event
});
/* Example */
$('body').on('mouseover', 'img', '{style: "red 2px solid"}', function(evt) {
$(this).css('border',evt.data.style);
}
/* adds a red border around each image on the page when the user mouses over the image */
delegate()
bind() only affects existing elements. delegate() continues to apply to elements added later
$('containerSelector').delegate('selector', 'event', function() {
//code to respond to event
});
/* Example */
$('body').delegate('img','mouseover',function() {
$(this).css('border','red 2px solid');
}
/* adds a red border around each image when the user mouses over the image */
Useful Tips
$()
is an alias for jQuery()
- either can be used. A selection can be saved into a variable. This can be more efficient if the selection is called multiple times in a script and can make the code easier to read and follow.
It may help to minimize the number of times you change the DOM by placing all html into a variable and updating the page only 1 time whenever possible.
// to create the variable
var myId = $('#myId');
// to use the variable
myId.html = "<p>Some new content here</p>";
It is a good practice to optimize selectors to make scripts faster and more efficient.
- Use ID selectors whenever possible - they are fastest, but only get one element.
- To retrieve multiple elements, use as a descendent of an ID whenever possible. For example, all links within #nav instead of all links on the entire page.
- Use the find() function.
- Avoid too much specificity - too many descendents run slowly. Use only what is necessary.
jQuery Docs
jQuery Docs is found at http://docs.jquery.com and contains tutorials, help, getting started, etc. You will also find the jQuery API (Application Programming Interface) which is a great resource for detailed information of all jQuery functions, attributes, events, etc.
Examples
Adding a click event for a shopping cart.
Working from the HTML, create a click event that will select information from the item clicked and place it in a virtual shopping cart is really a process of hiding, showing and keeping track of the items in play. Here is an example of how to create this functionality.
First, you have to have the HTML coded correctly for you to acquire the information needed to track the items being moved to and from the shopping cart. We are going toΒ create items that include an image, title, Add to Cart button and a star rating system. When an item is selected to be placed in the cart, the Add to Cart button should hide and the name of the item should appear in the cart with a new button for removing the item from the cart and placing it back "on the shelf".
<!-- Cart item -->
<figure>
<figcaption>Image Name</figcaption>
<img src="image1.jpg" class="flower" alt="Image Name">
<span class="rating">
<img src="staroff.gif"><img src="staroff.gif"><img src="staroff.gif"><img src="staroff.gif"><img src="staroff.gif">
</span><br>
<button class="add" id="image1" name="Image Name">Add to Cart</button>
</figure>
<!-- Cart list -->
<ul id="cart">
<li id="empty">Your shopping cart is empty</li>
</ul>
Next, we need to create a JS event to handle the changes. This event will add an item to the shopping cart and remove the "Add to Cart" button when an Add to Cart button is clicked. In order to add the related item to the cart, we need to collect the id and name of the item. We also need to check the cart to see if it is empty so we can hide the empty cart message if it is not.
/* Add to cart event */
$('.add').click(function () {
var itemName = $(this).attr('name');
var itemID = $(this).attr('id');
shoppingCart++;
if (shoppingCart > 0) {
$('#empty').hide();
}
var cartLink = "<li class='cartItem' name='" + itemID + "'>" + itemName + " <span class='del'>Remove</span></li>";
$('#cart').append(cartLink);
$('#' + itemID).hide();
});