← Back to Chapters

jQuery Element Selectors

? jQuery Element Selectors

? Quick Overview

jQuery element selectors allow you to select HTML elements based on their tag names such as p, div, span, ul, etc. Once selected, you can apply styles, events, animations, or manipulate content easily.

? Key Concepts

  • Select elements using HTML tag names
  • Apply operations to all matched elements automatically
  • Works similarly to CSS element selectors
  • Core part of jQuery DOM manipulation
  • Can be combined to select multiple types at once

? Syntax / Theory

The basic syntax for element selectors in jQuery is:

$("element") — selects all matching elements of that type.

➕ Grouping Selectors

You can also select multiple distinct elements at the same time by separating them with a comma:

$("h1, div, p") — selects all h1, div, and p elements.

? Code Example(s)

? View Code Example
// Select all paragraph elements and change text color
$("p").css("color","blue");
? View Code Example
// Hide all div elements on button click
$("button").click(function(){
$("div").hide();
});
? View Code Example
// Select multiple elements using group selector
$("h2, p, span").css("background-color","yellow");

? Live Output / Explanation

In the first example, all <p> elements become blue instantly. In the second example, clicking the button hides all <div> elements on the page.

? Interactive Playground

Use the buttons below to select specific HTML elements inside the canvas box. Watch how jQuery finds them!

Status: Waiting for selection...

I am a Header (h4) inside a Div

I am a <p> paragraph inside a div.

I am a <span> element. I am another <span>.
  • List Item 1 (<li>)
  • List Item 2 (<li>)
  • List Item 3 (<li>)

I am a standalone <p> paragraph.

? Previous Interactive Example (Code Only)

? View Code Snippet
// Toggle highlight on all list items
$("li").click(function(){
$(this).css("background","yellow");
});

? Use Cases

  • Styling multiple elements at once
  • Applying events to common tags
  • Quick DOM updates for repeated structures
  • Building interactive UI behaviors
  • Resetting forms or clearing areas

✅ Tips & Best Practices

  • Avoid broad selectors like div unless required
  • Combine element selectors with class or ID
  • Use element selectors for consistent layouts
  • Cache selectors for better performance

? Try It Yourself

  • Change font size of all headings
  • Hide all images on click
  • Add borders to table rows
  • Change background of list items