How does jQuery.add( ) work in jQuery?

jQueryWeb DevelopmentFront End Technology

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>
raja
Published on 16-Dec-2017 14:42:33
Advertisements