jQuery add()

Introduction

Add <p> and <span> elements to an existing group of elements (<h1>):

View in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("h1").add("p").add("span").css("background-color", "yellow");
});/*from ww  w. j a v a2 s  .  c o  m*/
</script>

</head>
<body>

<h1>Welcome</h1>
<p>A p element.</p>
<p>Another p element.</p>

<span>A span element.</span>
<span>A span element.</span><br><br>
</body>
</html>

The add() method adds elements to an existing elements.

$(selector).add(element,context)
Parameter
Optional
Description
element



Required.



a selector expression, or
a jQuery object, or
one or more elements or
an HTML snippet
context
Optional.
the point to begin matching



PreviousNext

Related