Syntax error jQuery appendTo() with Example

jQuery appendTo() with Example



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 −

Updated on: 2019-11-12T11:25:53+05:30

505 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements