jQuery add() Method

jQuery add() Method

The jQuery add() method adds elements to an existing group of elements.

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery add() Method</h1> <p>Click on the button</p> <button>Click Me</button> <script> $(document).ready(function(){ $("button").click(function(){ $("h1").add("p").css("color", "red"); }); }); </script> </body> </html>

Syntax

$(selector).add(selector); //or $(selector).add(element); //or $(selector).add(html); //or $(selector).add(selector, context)

Parameter Values

ValueExplanation
selectorSpecifies a string representing a selector expression to find additional elements to add to the set of matched elements.
elementSpecifies one or more elements to add to the set of matched elements.
htmlSpecifies an HTML fragment to add to the set of matched elements.
contextSpecifies the point in the document at which the selector should begin matching.

add() at Specific Point

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery add() Method</h1> <p>Click on the button</p> <button>Click Me</button> <script> $(document).ready(function(){ $("button").click(function(){ $("h1").css("background", "yellow").add("p").css("color", "red"); }); }); </script> </body> </html>

add() with clone()

Example

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> </head> <body> <h1>jQuery add() Method</h1> <p>Click on the button</p> <button>Click Me</button> <script> $(document).ready(function(){ $("button").click(function(){ $("p").clone().add("<span>You Clicked</span>").appendTo(document.body); }); }); </script> </body> </html>

Reminder

Hi Developers, we almost covered 99.5% of jQuery Tutorials with examples for quick and easy learning.

We are working to cover every Single Concept in jQuery.

Please do google search for:

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author