Mastering jQuery Selectors: A Complete Guide for Beginners

What Are Selectors?
Websites contain countless HTML elements like headings (h1), paragraphs (p), buttons, images, and input fields.
jQuery works by first selecting these HTML elements before performing actions on them.
In other words, it follows the sequence:
Select → Modify → Execute
For example, to change a button's text, you must first select the button. Similarly, to alter a background color, you need to select the target element first.
Thus, selectors are arguably the most critical concept in jQuery.
The Basic Structure of jQuery Selectors
All selectors follow this format:
$("selector");
For example, writing $('h1') selects all h1 elements.
After selection, you can chain multiple functions:
For instance,
$('h1').css('color', 'red');
This changes all h1 elements' text color to red.
Preparing HTML for Practice
Create this basic HTML structure first:
<h1 id="title">jQuery Study</h1>
<p class="text">First sentence</p>
<p class="text">Second sentence</p>
<button id="btn">Click</button>
All examples will use this structure.
Tag Selectors
The most basic selector type. Targets elements by their HTML tag name.
$('p')
This selects all paragraph (p) tags. To change text color:
$('p').css('color', 'blue');
Running this changes all paragraphs to blue.
ID Selectors
In HTML, an ID is a unique identifier for one element only.
HTML example:
<h1 id="title">jQuery Study</h1>
jQuery syntax uses a # symbol before the ID:
$('#title')
To change the title text, write:
$('#title').text('Hello.');Class Selectors
Use classes when applying the same style or functionality to multiple elements.
HTML example:
<p class="text">First</p>
<p class="text">Second</p>
jQuery syntax uses a . (period) before the class name:
$('.text')
To adjust font size:
$('.text').css('font-size', '24px');
Both paragraphs will now have the new size.
Selecting Multiple Elements at Once
Separate selectors with commas to target multiple elements:
$('#title, p')
This selects both h1 and p tags. To change background color:
$('#title, p').css('background', 'yellow');
Selecting All Elements
The asterisk (*) selects every element on the page:
$(*)
This can change all text to blue, for example:
$(*) .css('color', 'blue');
However, this is rarely used in real projects due to overly broad selection.
Parent/Child Selectors
Select elements based on HTML structure:
<div>
<p>First</p>
<p>Second</p>
</div>
To select all p tags inside div, use:
div p
This selects only the p elements within the div.
Child Selectors
To select only direct children, use the > symbol:
div > p
This targets only the immediate child p elements. Commonly used in nested structures.
Selecting the First Element
To select just the first element, use:
$("p:first")
Or alternatively,
$("p").first()
To change background color:
$("p:first").css("background", "pink");
Selecting the Last Element
To select the last element, use:
$("p:last")
Or,
$("p").last()
For example, to make the last paragraph red:
$("p:last").css("color", "red");
Selecting by Index
Use index numbers (starting from 0) to target specific elements:
$("p:eq(1)")
This selects the second paragraph (index 1).
Selecting Odd and Even Elements
To select even-indexed elements:
$("p:even")
For odd-indexed elements:
$("p:odd")
To set background color for even paragraphs:
$("p:even").css("background", "#eeeeee");
Attribute Selectors
Select elements based on attributes like input type:
<input type="text">
<input type="password">
To select text inputs only:
$("input[type='text']")
Password fields can be selected with:
$("input[type='password']")
Selecting Form Elements
Common form element selectors:
Text inputs:
$(':text')
Checkboxes:
$(':checkbox')
Radio buttons:
$(':radio')
Buttons:
$(':button')
These are frequently used in real-world development.
Checking the Number of Selected Elements
To find how many elements were selected, use .length:
$("p").length
For example:
console.log($("p").length);
This outputs the number of paragraph tags.
Method Chaining
A key jQuery feature: chain multiple actions in one line:
$('#title')
.css('color', 'blue')
.css('font-size', '40px')
.text('jQuery Started');
This performs multiple operations sequentially.
Common Mistakes
Mixing Up # and .
ID selectors use #:
$('#title')
Class selectors use .:
$('.title')
This is a frequent beginner error.
Selecting Nonexistent Elements
Selecting elements that don’t exist in HTML won’t cause errors, but no action will occur. Always double-check element names and spelling.
Case Sensitivity Errors
ID and class names must match exactly in case:
id="Title"
Using $('#title') will fail if the actual ID is capitalized.
Practice Exercises
Exercise 1
Select all h1 elements and change their text color to blue.
Exercise 2
Change the font size of paragraphs with class 'text' to 30px.
Exercise 3
Change only the first paragraph's color to red.
Exercise 4
Change the background color of the last paragraph to yellow.
Exercise 5
Select only text-type input fields and change their border color to blue.
Key Takeaways
Selectors are how you find HTML elements.
Tag selectors use the element name (e.g., 'p').
ID selectors require a # prefix (e.g., '#title').
Class selectors require a . prefix (e.g., '.text').
You can select multiple elements at once.
Use first, last, or eq() to target specific positions.
Attribute selectors let you filter by element properties.
Most jQuery functionality starts with selecting elements.
Conclusion
This lesson covered jQuery's core concept: Selectors. Mastering selectors lets you freely choose and modify HTML elements, making it easier to learn advanced topics like event handling, animations, and AJAX.
In the next lesson, we’ll explore controlling HTML and CSS dynamically through text changes, style updates, and attribute modifications—essential skills for building interactive web pages.
FAQ
Why are selectors important in jQuery?
Selectors let you target specific HTML elements to apply changes, making it the foundation for all jQuery operations.
What's the difference between # and . in selectors?
# is used before IDs (e.g., '#header'), while . is used before classes (e.g., '.highlight').
Korean Source
This article is an English localization of a Korean-language report from BlogScore. Original: [jQuery 강좌] 2강 jQuery 선택자(Selector) 완벽 이해하기