Given an HTML document and the task is to select the elements with different ID’s at the same time using JQuery.
Approach:
- Select the ID’s of different element and then use each() method to apply the CSS property on all selected ID’s element.
- Then use css() method to set the background color to pink to all selected elements.
- Display the text which indicates the multiple ID selectors.
Example 1: In this example, the elements of different ID’s are selected and background color of these elements are changed.
<!DOCTYPE HTML> <html> <head> <title> JQuery | Multiple ID selectors </title> <style> #GFG_DIV { background: green; height: 100px; width: 200px; margin: 0 auto; color: white; } </style> <script src = </script> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 19px; font-weight: bold;"> </p> <div id = "GFG_DIV"> This is Div box. </div> <br> <button onClick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color: green; font-size: 24px; font-weight: bold;"> </p> <script> $('#GFG_UP').text("Click on button to select multiple" + " ID's and change their background-color"); function GFG_Fun() { $("#GFG_UP, #GFG_DIV, #GFG_DOWN").each(function(){ $(this).css("background-color", "pink"); }); $('#GFG_DOWN').text("Background-color of all " + "elements is changed."); } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: In this example, the elements of different ID’s are selected and text color of these elements are changed.
<!DOCTYPE HTML> <html> <head> <title> JQuery | Multiple ID selectors </title> <style> #GFG_DIV { background: green; height: 100px; width: 200px; margin: 0 auto; color: white; } </style> <script src = </script> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 19px; font-weight: bold;"> </p> <div id = "GFG_DIV"> This is Div box. </div> <br> <button onClick = "GFG_Fun()"> click here </button> <p id = "GFG_DOWN" style = "color: green; font-size: 24px; font-weight: bold;"> </p> <script> $('#GFG_UP').text("Click on button to select multiple" + "ID's and change their Text color"); function GFG_Fun() { $("#GFG_UP, #GFG_DIV, #GFG_DOWN").each(function(){ $(this).css("color", "blue"); }); $('#GFG_DOWN').text("Text color of all elements is " + "changed."); } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to use JavaScript variables in jQuery selectors ?
- CSS | Selectors Complete Reference
- Advanced Selectors in CSS
- Wildcard Selectors (*, ^ and $) in CSS for classes
- CSS | Syntax and Selectors
- Which characters are valid in CSS class names/selectors?
- CSS Child vs Descendant selectors
- SASS | Placeholder Selectors
- Why are dashes preferred for CSS selectors / HTML attributes ?
- 10 CSS Selectors Every Developer Should Know
- JavaScript | Replace multiple strings with multiple other strings
- jQuery | multiple elements Selector
- jQuery | multiple classes Selector
- How to Add and Remove multiple classes in jQuery ?
- How to select an element with multiple classes using jQuery ?
- jQuery | jQuery.fx.interval Property with example
- jQuery | jQuery.fx.off Property
- jQuery | jQuery.support Property
- jQuery | jquery Property
- CSS | Multiple Columns
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

