jQuery multiple elements 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 interactions, and cross-browser JavaScript development.
There are two ways to select multiple elements using selectors:
- element selector
- * selector
Syntax:
- For element selector:
$("element1, element2, element3, ...")
- For * selector:
$("*")
Parameter:
- element: This parameter is required to specify the elements to be selected.
Example 1: Using element selector.
HTML
<!DOCTYPE html> < html > < head > < script src = </ script > < script > $(document).ready(function () { $("h2, div, span").css( "background-color", "green"); }); </ script > </ head > < body > < center > < h1 >Welcome to GeeksforGeeks</ h1 > < h2 >Geeks1</ h2 > < div >Geeks2</ div > < p >Geeks3</ p > < p >< span >Geeks4</ p > </ center > </ body > </ html > |
Output:
Example 2: Using * selector.
HTML
<!DOCTYPE html> < html > < head > < script src = </ script > < script > $(document).ready(function () { $("*").css( "background-color", "green"); }); </ script > </ head > < body > < center > < h1 >Welcome to GeeksforGeeks</ h1 > < h2 >Geeks1</ h2 > < div >Geeks2</ div > < p >Geeks3</ p > < p >< span >Geeks4</ p > </ center > </ body > </ html > |
Output:
Supported Browsers:
- Google Chrome 90.0+
- Internet Explorer 9.0
- Firefox 3.6
- Safari 4.0
- Opera 10.5