Mastering jQuery: From DOM Manipulation to Dynamic Interactivity

1.55K 0 0 0 0

Chapter 3: jQuery Selectors, Filters & Events

🔹 1. Why Selectors, Filters & Events Matter in jQuery

Selectors and event handlers are the core building blocks of jQuery.

  • Selectors let you target any HTML element with incredible precision.
  • Filters allow you to narrow down your selections.
  • Events let your application react to user input, clicks, and interactions.

Master these, and you can control every piece of your front-end like a pro.


🔹 2. jQuery Selectors – The Basics

jQuery uses CSS-style selectors to target HTML elements.

Common Selector Patterns

Selector Type

Example

Description

ID

$("#id")

Selects element with ID

Class

$(".class")

Selects all elements with class

Element

$("p")

Selects all <p> tags

Attribute

$("[type='text']")

Input fields of type text

All elements

"*"

Selects everything

$("#logo").hide();            // Hide element by ID

$(".btn").css("color", "red"); // Change text color for all buttons


🔹 3. Chaining Selectors

You can use combinations and hierarchy:

$("div .note")     // All elements with class 'note' inside a div

$("ul > li")       // Only direct children <li> of <ul>

$("input:checked") // Only checked checkboxes/radios


🔹 4. Advanced Filtering in jQuery

Filter Methods

Method

Example

Description

.first()

$("li").first()

First element only

.last()

$("li").last()

Last element only

.eq(index)

$("li").eq(2)

Element at index 2

.filter()

$("li").filter(".active")

Only elements with class "active"

.not()

$("li").not(".disabled")

Exclude matching elements

$("li").filter(":odd").css("background", "#f0f0f0");

You can also chain .find(), .children(), .parent() for DOM traversal.


🔹 5. Event Handling Basics

Syntax:

$(selector).on(event, childSelector (optional), function)

Example: Click Event

$("#submitBtn").on("click", function() {

  alert("Button was clicked!");

});

Other Events:

Event Type

Use Case

click

Button or link clicks

change

Form input change

hover

Mouse enters/exits area

focus/blur

Input focus in/out

submit

Form submission

keydown

Keyboard input


🔹 6. Event Delegation

In dynamic content (like added elements), direct binding doesn’t work.

Use event delegation instead:

$(document).on("click", ".dynamicBtn", function() {

  alert("You clicked a new button!");

});

This binds the event to a static parent (document), not the dynamically created child.


🔹 7. Prevent Default Behavior

Use .preventDefault() in event handlers to stop default browser actions.

$("a").on("click", function(e) {

  e.preventDefault();

  alert("Link clicked, but not followed.");

});


🔹 8. Toggle, Show, Hide Events with jQuery

jQuery allows instant UI interactivity:

$(".faq").on("click", function() {

  $(this).next(".answer").slideToggle();

});

Combine this with .addClass() and .removeClass() for styling changes.


🔹 9. Combining Selectors, Filters & Events

Example: Make only even list items clickable.

$("ul li").filter(":even").on("click", function() {

  $(this).css("background-color", "yellow");

});


🔹 10. Summary Table: jQuery Selectors & Events


Task

jQuery Code Example

Select by ID

$("#elementId")

Select by class

$(".btn")

Filter visible items

$(".item:visible")

Bind click event

$(".btn").on("click", function...)

Delegate events

$(document).on("click", ".new", fn)

Prevent link redirect

$("a").on("click", e => e.preventDefault())

Back

FAQs


1. What is jQuery?

jQuery is a fast, small JavaScript library that simplifies HTML document traversal, manipulation, event handling, and AJAX.

2. Is jQuery still relevant today?

Yes while modern frameworks exist, jQuery remains heavily used in WordPress, older projects, CMSs, and quick prototypes.

3. What are the benefits of using jQuery?

It shortens code, handles cross-browser issues, simplifies AJAX, and is beginner-friendly.

4. How do I include jQuery in a webpage?

  1. Use a CDN:


<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>

5. What is the difference between $(document).ready() and window.onload?

$(document).ready() runs when the DOM is ready; window.onload waits for full page (including images) to load.

6. Can I use jQuery with other JavaScript libraries?

$(document).ready() runs when the DOM is ready; window.onload waits for full page (including images) to load

7. Is jQuery free to use in commercial projects?

Yes its open-source under the MIT license.

8. What's the difference between jQuery and JavaScript?

jQuery is a library built with JavaScript to simplify tasks like DOM handling and AJAX.

9. Does jQuery work on mobile browsers?

Yes it works across all major desktop and mobile browsers.

10. Should I learn jQuery before JavaScript?

It’s better to learn JavaScript fundamentals first, but jQuery is easier to grasp early on for quick UI work.