What is add() in JQuery?

The add() method is a jQuery method that adds elements or groups of elements to the set of matched elements or the whole document.

The set of matched elements could be an element selector, a class selector, or an id selector.

When the element is matched, the add() method then adds the new elements specified to the new one.

Syntax

$(element_to_match).add(element_to_add, context_param)

Parameters

  • element_to_add: This is the element to add to the matched element.
  • context_param: This is an optional parameter that specifies the point in the document where the selector matching should start.

Code

In the example below, we used the add() method to add a paragraph to an h3 heading element.

Console

Explanation

In line 4, the API to use jQuery in the HTML is added.

In the code above, the p and span elements were added with the add() method, and the background color of yellow was added using the css() method.

Free Resources