English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery add() Method

jQuery Traversal Methods

The add() method adds elements to the existing element group.

The add() method adds elements to the entire document, or if a specificcontextParam,It adds elements only within the context element.

Syntax:

$(selector).add(element, contextParam)

Example

Add <span> element to the existing element group <p> within:

$("document").ready(function(){
  $("p").add("span").css("background-color", "lightgreen");
});
Test and see‹/›

Add <p> and <span> elements to the existing element group <h2> within:

$("document").ready(function(){
  $("h2").add("p").add("span").css("background-color", "lightgreen");
});
Test and see‹/›

Add a <p> element to the existing <div> element:

$("button").click(function(){ 
  $("div").add("<p>Hello world</p>").appendTo("div");
});
Test and see‹/›

Parameter Value

ParameterDescription
elementSpecify a selector expression, a jQuery object, one or more elements separated by spaces to be added to the set of matched elements
contextParamOptional) Specify the starting point for the selector expression to match in the document

jQuery Traversal Methods