jQuery selectors are the foundation of interacting with HTML elements using jQuery. They act as powerful tools for pinpointing specific elements on your web page for manipulation, animation, and dynamic effects. This chapter will equip you with the knowledge to master jQuery selectors, from the most basic to the most intricate.
$(element_name)
$("p")
selects all <p>
(paragraph) elements on the page.<p>
elements will be returned by jQuery, allowing you to perform operations on them collectively.$("#id_name")
$("#main-heading")
selects the element with the ID “main-heading”.$(".class_name")
$(".clickable-button")
selects all elements with the class “clickable-button”.$("h1, h2")
selects all <h1>
and <h2>
elements.$("ul li")
selects all <li>
(list items) that are descendants of <ul>
(unordered lists).[]
to select elements based on attribute presence or value.$("[href]")
selects all elements with an href
attribute (regardless of value).$("a[href='#top']")
selects all anchor tags (<a>
) with an href
attribute value of “#top”.$("[attribute|=value]")
$("input[type|=text]")
selects all input elements whose type
attribute starts with “text” (e.g., “text”, “textarea”).$("[attribute$=value]")
$("img[src$=.png]")
selects all image elements (<img>
) whose src
attribute ends with “.png” (indicating PNG format).$("[attribute!=value]")
$("div[id!='content']")
selects all <div>
elements that do not have the ID “content”.$("parent > child")
selects child elements directly under the parent.$("prev ~ next")
selects elements of class “next” that follow a sibling of class “prev”.$("form")
to select all form elements.$("input:text")
for text inputs, $("input:checkbox")
for checkboxes, etc.$("p").hide();
$(".highlight").css("background-color", "yellow");
$("button").click(function() { alert("Button clicked!"); });
By mastering jQuery selectors, you gain the power to precisely target and manipulate elements on your web page. This allows you to create dynamic and interactive user interfaces, enhancing the overall user experience. As you delve deeper into jQuery, you'll discover even more advanced techniques for fine-grained control and complex interactions. Remember, practice is key! Experiment with different selectors and scenarios to solidify your understanding.Happy coding !❤️