Jquery selectors
Advertisements
Selectors
jQuery selectors are used to find or select Html elements based on their id, classes, types, attributes, values of attributes etc.. This jQuery selector is based on css selector but it have some own custom selectors.
Selector in jQuery is start with $ sign and parentheses: $()
Syntax
$(selector).action()
Element Selector
jQuery element selector selects or find Html elements based on the element name. In below syntax you can select all <p> elements.
Syntax
$("p")
Example of element selector
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p").hide(); }); }); </script> </head> <body> <h2>jQuery Code</h2> <p>This is My first jQuery code</p> <button>Click me</button> </body> </html>
Output
jQuery Code
This is My first jQuery code
#id Selector
jQuery id selector finds elements with a specific id. In below syntax you can find or select all elements which have list id.
Syntax
$("#list")
Example of #id selector
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("#list").hide(); }); }); </script> </head> <body> <h2>jQuery Code</h2> <p id="#list">This is My first jQuery code</p> <button>Click me</button> </body> </html>
Output
jQuery Code
This is My first jQuery code
.class Selector
jQuery class selector finds elements with a specific class. In below syntax you can find or select all elements which have list class.
Syntax
$(".list")
Example of .class selector
<!DOCTYPE html> <html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $(".list").hide(); }); }); </script> </head> <body> <h2>jQuery Code</h2> <p id=".list">This is My first jQuery code</p> <button>Click me</button> </body> </html>
Output
jQuery Code
This is My first jQuery code
Examples of jQuery Selectors
- $("*") : Selects all elements.
- $(this) : Selects the current Html element.
Google Advertisment