How does jQuery.add( ) work in jQuery?


If you want to add element to an existing group of elements, then use the add() method.

S.No
Parameter
Description
1.
element
Set the element, a jQuery object, one or more than one elements or HTML snippet, which you want to add to the existing group of elements
2.
context
This parameter is optional. It sets the point at which the selector expression begins matching.

Example

You can try to run the following code to learn how to work with jQuery.add() method −

Live Demo

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
  $(document).ready(function(){
    $("h1").add("p").css("background-color", "gray");
  });
</script>

</head>
<body>

<h1>Tutorialspoint</h1>
<p>Text Tutorials for free.</p>
<span>Video Tutorials for free.</span>

</body>
</html>

Updated on: 14-Feb-2020

94 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements