jQuery Tutorial Tutorials - jQuery .add() method

jQuery .add() method

The .add() method in jQuery is used to add a newly created jQuery object with elements to the set of matched elements.

jQuery .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

jQuery .add() method Example

example

Create 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>]

Try now

Date:2019-08-26 13:13:57 From:www.Lautturi.com author:Lautturi