How to select elements by attribute in jQuery ? Last Updated : 20 Sep, 2019 Summarize Comments Improve Suggest changes Share Like Article Like Report jQuery is a lightweight JavaScript library. In the vanilla JavaScript language, the getElementById method is used to select an element. However, jQuery provides a much lighter alternative for the same purpose. The 'jQuery Selector' allows the user to manipulate HTML elements and the data inside it(DOM manipulation). Syntax $("[attribute=value]") Here, attribute and value are mandatory. Some of the most commonly used jQuery selectors Syntax Example Selection * $("*") All elements on the webpage #id $("#geeks") Elements with id="geeks" .class $(".geeks") All elements with class="geeks" :first $("p:first") First 'p' element of the webpage :header $(":header") All the header elements like h1, h2, h3, h4 etc :empty $(":empty") All the empty elements :input $(":input") All the input elements like text, password etc :text $(":text") Input elements with type="text" :last-child $("p:last-child") Those 'p' elements that are the last child of their parents :animated $(":animated") All the animated elements on the webpage #id Selector: The #id selector specifies an id for an element to be selected. It should not begin with a number and the id attribute must be unique within a document which means it can be used only one time. Syntax: $("#example") The id selector must be used only when the user wants to find a unique element. Example: html <!DOCTYPE html> <html> <head> <title> How to select elements from attribute in jQuery ? </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> <script> $(document).ready(function() { $("button").click(function() { $("#para").hide(); }); }); </script> </head> <body> <h2>GeeksforGeeks</h2> <p>This is a constant paragraph.</p> <p id="para"> This paragraph will get hidden once the button is clicked. </p> <button>Click me</button> </body> </html> Output: Before Click on the Button: After Click on the Button: .class selector: The .class selector specifies the class for an element to be selected. It should not begin with a number. It gives styling to several HTML elements. $(".example") Example: html <!DOCTYPE html> <html> <head> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"> </script> <script> $(document).ready(function() { $("button").click(function() { $(".test").hide(); }); }); </script> </head> <body> <h2 class="heading">GeeksForGeeks</h2> <p class="test">This is a paragraph.</p> <p class="test">This is another paragraph.</p> <button>Click me</button> </body> </html> Output: Before Click on the Button: After Click on the Button: :first Selector: It is a jQuery Selector that is used to select the first element of the specified type. Syntax: $(":first") Example: html <!DOCTYPE html> <html> <head> <title>jQuery :first selector</title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <script> $(document).ready(function() { $("p:first").css( "background-color", "green"); }); </script> </head> <body> <h1>GeeksforGeeks</h1> <p>jQuery</p> <p>JavaScript</p> <p>PHP</p> </body> </html> Output: Comment More infoAdvertise with us Next Article How to select elements by attribute in jQuery ? S sanchit496 Follow Improve Article Tags : Web Technologies JQuery Similar Reads How to select element by ID in jQuery ? In this article, we are going to see how to select the element by its id using jQuery. To learn about this topic one should have prior knowledge about HTML, CSS, JavaScript and jQuery. Using JavaScript: This question is also solved by a popular JavaScript method called document.getElementById() whic 2 min read How to get custom element attribute data in jQuery ? Apart from attributes, we can also use custom element attributes. In this article, we will learn how to get the value of a custom attribute in an HTML element using jQuery. What is Attribute? The HTML attribute provides information about the element. It decides the behavior of an element. Examples o 3 min read How to select an element by name with jQuery ? Selecting an element by name with jQuery allows you to target HTML elements using their `name` attribute. This method is commonly used in form handling, where input fields, checkboxes, or radio buttons share the same name, enabling you to easily manipulate or retrieve their values.Table of ContentUs 3 min read How to select elements by data attribute using CSS? CSS provides a way to select HTML elements based on attributes and their values. This can be extremely helpful for targeting elements in a flexible and specific way. This enables fine-tuned control over styling elements in a webpage.Here are the different ways to select elements using attribute sele 2 min read How to select all ancestors of an element in jQuery ? In this article, we will select all the ancestor elements of an element in jQuery. To select all ancestor elements of an element, the parents() method is used. This method is used to find all the parent elements related to the selected element. This method traverses all the levels up the selected el 1 min read Like