Identifying Elements with jQuery

Objective

After completing this lesson, you will be able to identify HTML elements using id, class, and location.

How to Inspect an Element?

Inspecting an element is as easy as 1-2-3! To inspect an element, follow these steps:

  1. Open any website on your Chrome browser.
  2. Right-click on the element that you want to inspect and click Inspect.
  3. The element will be highlighted on the screen in the developer panel.

Watch the video to understand how to inspect an element.

Identify an Element with Attributes (Id and Class)

In the first lesson, we learned that the HTML of a site is comprised of three components: elements, attributes, and values.

When building a jQuery selector, we can use an element's attribute to identify a specific element. The two most common attributes you will encounter when inspecting an element are ids and classes. Let's take a look at an element that we encountered in the first lesson.

Image showing HTML Code

We were able to identify that this element had two attributes: id and class. Now let's translate this into a jQuery selector.

Building the jQuery Using Id and Class

The attribute id and class, when written as a jQuery selector, are translated into the following:

ElementSelectorValueMeaning
ul#create-accountThe id attribute specifies a unique id for an HTML element (the element must be unique within the HTML document).
ul.nav flThe class attribute specifies one or more class names for an element.In this example, the ul element has two classes, separated with a space in the HTML code: nav and fl.
Writing the jQuery Selector

Now that we have our jQuery selector, let's write it out!

To identify the ﹤ul﹥ element using its id, we will write the following selector:

ul#create-account

To identify the ﹤ul﹥ element using its class, we will write the following selector:

ul.nav.fl

Note

If an attribute has multiple values, you will need to insert the selector between those values.

For example, ﹤div class="top clear"﹥ will be translated to the following selector:

div.top.clear

Now that you have a general idea of how to build a jQuery selector with id and class, let's delve a little deeper into this!

The following table contains an element in the left column along with its corresponding jQuery selector in the right one.

ElementjQuery Selector
nav id="navigation"nav#navigation
li class="menu-item"li.menu-item
div class="page col-full"

div.page.col-full

Remember: If there is a space between words in a class, that means that the element has multiple classes.

ul class="nav fr parent"ul.nav.fr.parent
div id="content-header"div#content-header
h1 class="product_title entry-title"h1.product_title.entry-title

Identify an Element with Location

Now that you can build a jQuery selector of an element using its id or class attributes, let's learn how to use an element's location to write a jQuery selector.

Why should I use the element's location?

Sometimes using the element's class or id alone isn't enough to build a unique jQuery selector. There may be instances where the element's attributes aren't specific enough or the element might not have any attributes at all. When this happens, you will need to use the element's location.

To build a jQuery selector by location, you will first need to refer to its associated elements.

  • Parent: The element located above the child element in the HTML structure.
  • Child: A child element is located right below the parent element in the HTML structure.
  • Descendant: A descendant element is located anywhere below the parent element in the HTML structure.

In general, an element can fall under multiple categories from the list above.

For example, take a look at your family tree. You are not only a child of your parent, but also the descendant of your grandparent. In turn, your parent is also a child of their parent and so on.

Like a family tree, the way elements can be categorized is dependent on the context and on what the element is related to.

Breaking Down Location

Let's look at the example below to get a better understanding of element location:

Image shows HTML code with element locations.

If we focus on div#AppBodyHeader in relation to div#pageBody, div#AppBodyHeader is the child element of div#pageBody as it is nested directly below that element.

However, if we focus on div#AppBodyHeader in relation to td.label, div#AppBodyHeader is the the parent element of td.label as it is nested directly above that element.

Identify an Element with Attribute and Location

So far you've learned how to identify elements using attributes and how to identify elements using their location. Now, let's combine both!

Using Attributes and Location

Let's look at a previous HTML example to identify an element using attribute and location.

This image shows HTML code using attributes and location.

Let's say, for example, that we want to use td.label as our jQuery selector. Easy enough, right?

However, if you take a closer look at the example above, you see that there are two td.label elements in the HTML. So how can we uniquely identify one td.label element over the other?

To uniquely identify an element, we will need to combine both the element's attributes and location.

How to Use Attribute and Location?

Let's try to identify the highlighted element below.

This image shows an HTML code with a highlighted element

In this scenario, we also need to take into consideration the parent element of td.label so we can differentiate it from the first one. To do this, we will use a [space].

Using a [space] tells our selector to look for the child or descendant of an element. This means we will look for "children" of the first element that match the second element. This is used to find an element based on one of its "parents".

For the example above, to uniquely identify the second td.label, we will write the following jQuery selector:

div#contentWrapper td.label

This means that we are looking at the child element of div#contentWrapper that matches td.label. To put it alternatively, we are looking at the td.label element whose parent is div#contentWrapper.

Log in to track your progress & complete quizzes