0Pricing
jQuery Academy · Lesson

Selecting Element with jQuery

Selecting Element with jQuery

Selecting Element with jQuery is a free jQuery Academy lesson on CoddyKit — lesson 1 of 1. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the jQuery Academy learning path, one of 1 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Introduction

Hello,

jQuery uses the XPath syntax to select DOM elements.

So because you are using CSS or if you have used XPath to select nodes in XML, you already know how to select elements with jQuery!

Let's use them with jQuery 🚀

Class Selector

Class Selector

The element in the desired class is selected using the dot(.) operator.

In the example below, It will select all elements and change color which have the title class. 

<p class="title">Hello</p>
<p class="title">CoddyKit</p>
<p class="title">Learning</p>
<p class="title">jQuery</p>

<script>
$('.title').css('color','pink')
</script>

WARNING

we will introduce css() method in the following lessons. 

For now, just know that the css() method is used to define the style of the element

Id Selector

Id Selector

The element in the desired id can be selected using the #(hash mark) operator. As we know, the same id value can be given to only one element in the HTML page.

In the example below, It will select the element and change the color, which has the title id.

<p id="title">Hello</p>
<p id="title1">CoddyKit</p>
<p id="title2">Learning</p>
<p id="title3">jQuery</p>

<script>
$('#title').css('color','pink')
</script>

Tag Selector

Tag Selector

The element in the desired tag can be selected using the just tag name

In the example below, It will select the element and change the color, which has the span tag.

<p>Hello</p>
<span>Hello</span>
<span>CoddyKit</span>

<script>
$('span').css('color','pink')
</script>

Children Selector

Child Selector

If we want to select the children of an element, we use the greater than(>) operator.

In the example below, It will select the all children of the desired element.

<p>
  <span>Hello</span>
  <span>CoddyKit</span>
</p>

<script>
$('p>span').css('color','pink')
</script>

Attribute Selector

Attribute Selector

We can select the desired element by checking if an element's attribute has a value.

In the example below, It selects the elements that have the <a> tag and the target attribute with value _blank.

<a target="_blank" 
     href="http://coddykit.com">
   CoddyKit
 </a>

<script>
$ ('a[target = "_blank"]').css('color','pink')
</script>

jQuer Selection Methods

jQuery makes it easier to select in the DOM by providing some methods as well as XPath selectors.

Let's learn about them.

Parent Method

parent() Method

We can select the desired element's parent by using the parent() method that provided by jQuery.

In the example below, It selects the element's parent and changes the parent element's text color.

<div>
Parent Element Text
<a target="_blank"  href="http://coddykit.com">
   CoddyKit
 </a>
</div>

<script>
$ ('a').parent().css('color','pink')
</script>

Children Method

children() Method

We can select the desired element's all children by using the children() method that provided by jQuery.

In the example below, It selects the element's all children and changes the chidlren's text color.

<div>
<a target="_blank"  href="http://coddykit.com">
   CoddyKit
 </a>
<span>Children Element Text</span>
</div>

<script>
$ ('div').children().css('color','pink')
</script>

parents method

parents() Method

We can select the desired element's ascendant element by using the parents() method that provided by jQuery.

In the example below, It selects the element's ascendant and changes the parent element's text color.

The difference between this method from other methods that we learned is that it can have a parameter.

So we send an XPath selector as a parameter to define which ancestor to be chosen.

<div class="wrapper">
Hello
<p>
<span>CoddyKit</span>
<p>
</div>

<script>
$ ('span').parents('.wrapper').css('color','pink')
</script>

next method

next() Method

The next() method selects the next sibling of the desired element.

In the example below, It selects the next first sibling of the element with the tag a.

<a href="http://coddykit.com">CoddyKit</a>
<span>I am first sibling of a element</span>

<script>
$ ('a').next().css('color','pink')
</script>

nextAll method

nextAll() method

The nextAll() method is used to select the next siblings of the desired element.

In the example below, it selects all siblings of the element with the div tag after it.

<span>I'm previous sibling<span>
<div></div>
<span>I'm next sibling<span>
<span>I'm next sibling too<span>


<script>
$ ('div').nextAll().css('color','pink')
</script>

siblings method

siblings() Method

You can select all desired element's siblings by using the siblings() method.

In the example below, it selects all siblings of elements with the p tag.

<p>Hello</p>
<div>jQuery</div>
<div>Methods</div>

<script>
$ ('p'). siblings().css('color','pink')
</script>

prev

prev() Method

prev() method is used to select the previous sibling of an element.

Selects the preceding first sibling of the element with the tag a.

<span>Hello Coddy</span>
<div class="test"></div>

<script>
$ ('.test').prev().css('color','pink')
</script>

prevAll Method

prevAll() method

The prevAll() method is used to select the previous siblings of the desired element.

In the example below, it selects all siblings of the element with the div tag before it.

<span>I'm previous sibling</span>
<span>I'm previous sibling too</span>
<div></div>
<span>I'm next sibling</span>

<script>
$('div').prevAll().css('color','pink')
</script>

Congratulations

Congratulations 🎉

This section taught us to select DOM elements.

In the next section, We will continue to learn about DOM manipulations.

Selecting Element with jQuery — illustration 17

Frequently asked questions

Is the “Selecting Element with jQuery” lesson free?

Yes — the full text of “Selecting Element with jQuery” is free to read here on the web, and the jQuery Academy course includes 1 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the jQuery Academy course, upgrade to CoddyKit PRO.

What will I learn in “Selecting Element with jQuery”?

Selecting Element with jQuery You practise jQuery Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start jQuery Academy?

No prior experience is required. jQuery Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 1, so you can start here or from the beginning and move at your own pace.

How long does the “Selecting Element with jQuery” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this jQuery Academy lesson?

Yes. Every jQuery Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

← Back to jQuery Academy