How jQuery Selectors are Executed ?
Last Updated :
05 Jun, 2023
jQuery is a fast, lightweight JavaScript library designed to simplify HTML DOM tree traversal, manipulation, event handling, CSS animation, and Ajax. The main objective of jQuery is to provide an easy way to use JavaScript on your website to make it more interactive and attractive.
It is widely famous for its philosophy of “Write less, do more.” because it was developed for performing more tasks with few lines of code.jQuery is faster than JavaScript because it has less code as compared to JavaScript.
Syntax:
$(selector).action()
- $ - It is to define jQuery.
- selector - It is to find HTML elements in an HTML page.
- action() - It is the action to be performed on the selected elements.
jQuery selector: jQuery selectors are used to select the HTML element(s) and allow you to manipulate the HTML element(s) in the way you want. It selects the HTML elements on a variable parameter such as their name, classes, id, types, attributes, attribute values, etc.
Example: In this example, we will select an HTML element and manipulate the element using jQuery.
HTML
<!DOCTYPE html>
<html>
<head>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<style>
body {
color: green;
text-align: center;
}
</style>
<script>
$(document).ready(function () {
$("button").click(function () {
$("p").hide();
});
});
</script>
</head>
<body>
<h1>GeeksForGeeks</h1>
<p>paragraph one.</p>
<p>paragraph two.</p>
<button>Click me to hide paragraphs</button>
</body>
</html>
Output:
jQuery execution
If dealing with more than two selectors in a row then your last selectors are always executed first.
For example, jQuery will first find all the elements with class “.list” and then it will select all the elements with the id “second”.
$('p').html($("second.list").text());
To know more about jQuery selectors you can follow this jQuery Selectors Complete Reference
Similar Reads
jQuery [attribute] Selector The [attribute] Selector is an inbuilt selector in jQuery, used to select all the elements with the specified attribute. Syntax: $("[attribute_name]") Parameter: attribute_name: It is the required parameter which specify the attribute to be select. Example-1: html <!DOCTYPE html> <html>
1 min read
jQuery element Selector jQuery is an open-source JavaScript library that simplifies the interactions between an HTML/CSS document, or more precisely the Document Object Model (DOM), and JavaScript. Elaborating the terms, it simplifies HTML document traversing and manipulation, browser event handling, DOM animations, Ajax i
1 min read
jQuery :enabled Selector The jQuery :enabled selector is used to select all enabled form elements. Syntax : $(":enabled")Parameter: :enabled selector: It is used for selecting HTML elements that support the disabled attribute. i.e button tag, input tag, optgroup tag, option tag, select tag and textarea tag.Example 1: HTML
1 min read
jQuery #id Selector jQuery is an open-source JavaScript library that simplifies the interactions between an HTML/CSS document, or more precisely the Document Object Model (DOM), and JavaScript. Elaborating on the terms, it simplifies HTML document traversing and manipulation, browser event handling, DOM animations, Aja
1 min read
jQuery :first Selector The jQuery :first Selector is used to select the first element of the specified type. Syntax: $(":first")Example 1: In this example, we will select the first <p> element by using jQuery :first Selector. HTML <!DOCTYPE html> <html> <head> <script src= "https://ajax.goo
1 min read