How to find elements from HTML using jQuery
In this article, you will learn about jQuery selectors and how to find DOM element(s) using selectors.
The jQuery selector provides functionality to find DOM elements in your web page. jQuery selectors are one of the most important parts of the jQuery library.
All selectors in jQuery start with the dollar sign and parentheses: $().
jQuery Selector Syntax
$(selector expression)
Example 1 : The .class Selector
The jQuery .class selector allow you to finds all the elements with a specific class.
If you want to find all the elements with class name "post-title" then the syntax will be as following.
$(".post-title")
Example 2 : The #Id Selector
The jQuery #Id selector allow you to finds the particular element with a specific Id.
The Id selector will be used when you want to find single element from web page.
$(document).ready(function(){
$("#btn-hide-title").click(function(){
$(".post-title").hide();
});
});
In above example, when a user click (button hide title) all the elements with class name "post-title" will be hidden.
jQuery Selector Patterns
$(":text") | Selects all input elements where type="text" |
$("input") | Selects all input elements |
$(":button") | Selects all input elements where type="button" |
$(":visible") | Selects all visible elements |
$(":hidden") | Selects all hidden elements |
$(":enabled") | Selects all enabled elements |
$(":disabled") | Selects all disabled elements |
$(":checked") | Selects all checked input elements |
$("tr:first-child") | Selects the first tr element |
$("tr:last-child") | Selects the last tr element |
$("div[class='test']") | Find all the div elements whose class attributes is test |
$(this) | Select the current element |
$("input[name='radio-gender']") | Find the radio button whose name attribute is radio-gender |
Comments
Post a Comment