Home  >  jQuery

Example of .add() in jQuery

February 16, 2013
.add() in jQuery can add DOM elements logically together with which we can apply any css or any addclass for all those DOM elements that have been added by .add() method. As an arguments .add() can accept the below parameters.
.add( selector )
.add( elements )
.add( html )
In the below example we have taken three DOM element. One is p and b and another is span. I need to assign background color red to all the three of these DOM element. We can seperatly give the color or smartly we can use .add() method of jquery.
<!DOCTYPE html>
<script src="http://code.jquery.com/jquery-latest.js"></script>

<span>name is</span>

<script>$("p").add("span").add("b").css("background", "red");</script>

Run the program and output will look like


name is Atul

©2022 concretepage.com | Privacy Policy | Contact Us