jQuery appendTo() with Example

jQueryWeb DevelopmentFront End Technology

The appendTo() method in jQuery is used to insert HTML elements at the end of the selected elements.

Syntax

The syntax is as follows −

$(content).appendTo(selector)

Above, content is the content to be inserted.

Example

Let us now see an example to implement the jQuery appendTo() method −

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
   $(document).ready(function(){
      $("button").click(function(){
         $("<em>New</em>").appendTo("h3");
      });
   });
</script>
</head>
<body>
<h1>Blog Posts</h1>
<h3>How to install Java?</h3>
<h3>How to install Ruby?</h3>
<button>Click me to check new and old post status</button>
</body>
</html>

Output

This will produce the following output −

Now, click on the button above −

raja
Published on 12-Nov-2019 11:25:23
Advertisements