Skip to Content
Learning from jQuery
book

Learning from jQuery

by Callum Macrae
January 2013
Beginner to intermediate
116 pages
2h 15m
English
O'Reilly Media, Inc.
Content preview from Learning from jQuery

Chapter 3. DOM Traversal and Manipulation

The Document Object Model (DOM for short) is the API provided by the browser to enable you to view and interact with objects (such as elements) in HTML and XML documents. jQuery includes a number of functions that make working with the DOM a lot easier than with JavaScript alone, which can be pretty ugly. However, the functions provided by jQuery can be rather hefty (especially in older browsers), and it is often a lot faster to just use pure JavaScript. Therefore, it is important to know how both work.

Selecting an Element

In jQuery, there is only really one way to select an element, and that is with the CSS selector:

$('#foo');

In JavaScript, there are several ways. You can select elements with their CSS selector (which I will cover later), but it isn’t supported in Internet Explorer versions earlier than 8, so I will cover the traditional methods first.

You can select an element by its Id, ClassName, or TagName. The syntax is pretty similar for all of them:

document.getElementById('foo');
document.getElementsByClassName('bar');
document.getElementsByTagName('p');

The first line, document.getElementById, gets the element with ID foo. As there can only be one element associated with each ID, there is no need to return a NodeList here, and it will return either the element or null.

The second line, document.getElementsByClassName, gets all elements with “bar” as one of their classes. It will return the elements as a NodeList, which is similar to ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Beyond jQuery

Beyond jQuery

Ray Nicholus
jQuery Cookbook

jQuery Cookbook

Cody Lindley
Learning jQuery 3 - Fifth Edition

Learning jQuery 3 - Fifth Edition

Adam Boduch, Jonathan Chaffer, Karl Swedberg

Publisher Resources

ISBN: 9781449335182Errata Page