
- jQuery - Home
- jQuery - Roadmap
- jQuery - Overview
- jQuery - Basics
- jQuery - Syntax
- jQuery - Selectors
- jQuery - Events
- jQuery - Attributes
- jQuery - AJAX
- jQuery CSS Manipulation
- jQuery - CSS Classes
- jQuery - Dimensions
- jQuery - CSS Properties
- jQuery Traversing
- jQuery - Traversing
- jQuery - Traversing Ancestors
- jQuery - Traversing Descendants
- jQuery References
- jQuery - Selectors
- jQuery - Events
- jQuery - Effects
- jQuery - HTML/CSS
- jQuery - Traversing
- jQuery - Miscellaneous
- jQuery - Properties
- jQuery - Utilities
- jQuery Plugins
- jQuery - Plugins
- jQuery - PagePiling.js
- jQuery - Flickerplate.js
- jQuery - Multiscroll.js
- jQuery - Slidebar.js
- jQuery - Rowgrid.js
- jQuery - Alertify.js
- jQuery - Progressbar.js
- jQuery - Slideshow.js
- jQuery - Drawsvg.js
- jQuery - Tagsort.js
- jQuery - LogosDistort.js
- jQuery - Filer.js
- jQuery - Whatsnearby.js
- jQuery - Checkout.js
- jQuery - Blockrain.js
- jQuery - Producttour.js
- jQuery - Megadropdown.js
- jQuery - Weather.js
jQuery multiple elements Selector
The multiple elements selector in jQuery allows you to select multiple HTML elements using a single jQuery selector. To do so, we need to seperate each element with a (,) comma.
Syntax
Following is the syntax to define multiple elements in jQuery −
$("element1,element2,element3,...")
Parameters
The "element1,element2,element3,..." specifies the elements to be selected.
Example 1
In the following example, we are selecting all <p> and <div> elements using the element selector −
<html> <head> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p, div").css("background-color", "yellow"); }) }); </script> </head> <body> <p>Paragraph element.</p> <div>Div element.</div> <h3>Heading element.</h3> <p>Paragraph element.</p> <div>Div element.</div> <h3>Heading element.</h3> <button>Click</button> </body> </html>
When we click the button, all the <p> and <div> elements will be selected and highlighted with yellow color background.
Example 2
In this example, we are selecting all the <p>, <div> and <h3> and hides them when the button is clicked −
<html> <head> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("p, div, h3").hide(); }) }); </script> </head> <body> <p>Paragraph element.</p> <div>Div element.</div> <h3>Heading element.</h3> <p>Paragraph element.</p> <div>Div element.</div> <h3>Heading element.</h3> <button>Click</button> </body> </html>
After clicking the button, all the selected elements (<p>, <div> and <h3>) will be hidden from the DOM.
Example 3
Here, we are using the "*" selector to select all the elements in the DOM −
<html> <head> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script> $(document).ready(function(){ $("button").click(function(){ $("*").hide(); }) }); </script> </head> <body> <p>Paragraph element.</p> <div>Div element.</div> <h3>Heading element.</h3> <p>Paragraph element.</p> <div>Div element.</div> <h3>Heading element.</h3> <button>Click</button> </body> </html>
After clicking the button, all the selected elements in the DOM will be hidden.