Skip to main content

jQuery Selectors

jQuery is a popular JavaScript library that provides an easy-to-use API for selecting and manipulating elements in the Document Object Model (DOM) of a web page.

Here are some common DOM selectors in jQuery:

$("element")

This selector returns all elements that match the specified tag name.

var elements = $("div");

$(".class")

This selector returns all elements that have the specified class.

var elements = $(".myClass");

$("#id")

This selector returns the element with the specified ID.

var element = $("#myElement");

$("selector")

This selector returns all elements that match the specified CSS selector.

var elements = $("div.myClass");

$("parent child")

This selector returns all elements that match the specified CSS selector and are descendants of a specified parent element.

var elements = $("ul li");

$("selector:first")

This selector returns the first element that matches the specified CSS selector.

var element = $("div:first");

$("selector:last")

This selector returns the last element that matches the specified CSS selector.

var element = $("div:last");

$("selector:eq(index)")

This selector returns the element at the specified index that matches the specified CSS selector.

var element = $("div:eq(2)");

$("selector:odd")

This selector returns all odd-indexed elements that match the specified CSS selector.

var elements = $("div:odd");

$("selector:even")

This selector returns all even-indexed elements that match the specified CSS selector.

var elements = $("div:even");