The jQuery add() method is used to add elements to the existing group of elements. This method can add element to the whole document, or just inside the context element if the context parameter is defined.
Syntax:
$(selector).add(element, context_parameter)
Here selector helps to find the matching element.
Parameters: It accepts two parameters that are specified below:
- element: It is the selected element.
- context_parameter: It is the context parameter of the element defined.
jQuery code to show the working of add() method:
HTML
<!DOCTYPE html>
< html >
< head >
< script src =
</ script >
< script >
$(document).ready(function () {
$(".heading").add("#para1").add("#span1").
css("background-color", "lightgreen");
});
</ script >
</ head >
< body >
< p style = "color:green" class = "heading" >
Welcome to GfG!!!
</ p >
< p style = "color:green" id = "para1" >
Welcome to GeeksforGeeks !!!
</ p >
< span style = "color:green" id = "span1" >
Article 1 !!!.
</ span >
< div >This example adds the same css style
for both "p" and "span" elements,
using its class and id name!!!
</ div >
</ body >
</ html >
|
Output:

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
09 Sep, 2023
Like Article
Save Article