The appendTo() method in JQuery is used to insert HTML element at the end of an HTML element.
<html>
<head>
<title> My First Programme </title>
</head>
<style>
div {
color: yellow;
}
p {
background-color: violet;
}
</style>
<body>
<h1> JQuery </h1>
<p>New Code</p>
<button> Click to set a new value </button>
<script src = "https://ajax.googleapis.com/ajax/libs/jquery/3.7.1/jquery.min.js"> </script>
<script>
$('button').click( function() {
$('<div> plus another div element </div>').appendTo("p");
});
</script>
</body>
</html>
So, in the above code, we have to to add a new HTML content to the <p> element,
<div> plus another div element </div>
And make code like,
<p> New Code <div> plus another div element </div> </p>
And we have done it using the JQuery statement,
$('button').click( function() {
$('<div> plus another div element </div>').appendTo("p");
});So, on button click, the appendTo() function is called,
$('<div> plus another div element </div>').appendTo("p");And the content of <p> element,
<p>New Code</p>
Is added with,
<div> plus another div element </div>
And make to code like,
<p> New Code <div> plus another div element </div> </p>
We have added styles to <div> and <p> elements.
<style>
div {
color: yellow;
}
p {
background-color: violet;
}
</style>So that you can understand, how the code looks like after adding <div> element.