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>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
<p>My</p>

<span>name is</span>

<b>Atul</b>
<script>$("p").add("span").add("b").css("background", "red");</script>
</body>
</html>
 
Run the program and output will look like

My

name is Atul
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us