.add()
methodThe .add()
method in jQuery is used to add a newly created jQuery object with elements to the set of matched elements.
.add()
method Syntax.add(selector) .add(elements) .add(html) .add(selection) .add(selector,context)
Parameter | Type | Description |
---|---|---|
selector |
Selector | A string representing a selector expression to find additional elements |
elements |
Element | One or more elements |
html |
htmlString | An HTML fragment |
selection |
jQuery | An existing jQuery object with elements |
context |
Element | The point in the document at which the selector should begin matching |
.add()
method ExampleCreate a new jQuery object to the set of the matched elements. This element in the jQuery object should match the selector expression #brand
HTML
<p>Hello</p><span id="brand">Lautturi</span>
jQuery
// use selector "#brand" to get elements ,then add them to the set of "<p>" matched elements var $set = $("p").add("#brand"); // highlight all the elements in the new set. $set.each(function(){ $(this).css('background','cyan'); })
Result
[<p>Hello</p>,<span id="brand">Lautturi</span>]