Jquery appendTo And prependTo Example

Websolutionstuff | Dec-13-2021 | Categories : jQuery

In this article we will see jquery appendTo() and prependTo example. The appendTo() method inserts HTML elements at the end of the selected elements. The prependTo() method inserts HTML elements at the beginning of the selected elements. The .append() and .appendTo() methods perform the same task. The major difference is in the syntax. In .prepend() and .prependTo() methods perform the same task. The major difference is in the syntax.

The .append() method selector expression preceding the method is the container into which the content is inserted. With .appendTo(), on the other hand, the content precedes the method, either as a selector expression or as markup and it is inserted into the target container.

The .prepend() methos selector expression preceding the method is the container into which the content is inserted. With .prependTo(), on the other hand, the content precedes the method, either as a selector expression or as markup. and it is inserted into the target container.

Example : appendTo() Method

Syntax : 

$(content).appendTo(selector);

In this example, Insert span element at the end of each P tag when click on button.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("<span> Hello, Websolutionstuff !!</span>").appendTo("p");
  });
});
</script>
</head>
<body>

<h3>Jquery appendTo and prependTo Example - Websolutionstuff</h3>

<p>This is a paragraph.</p>

<button>Insert span element at the end of each P tag</button>

</body>
</html>

Output : 

jquery_appendto_example

 

 

Example : prependTo() Method

Syntax :

$(content).prependTo(selector);

In this example,Insert span element at the beginning of P tag when click on button.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $("<span>Hello, Websolutionstuff !! </span>").prependTo("p");
  });
});
</script>
</head>
<body>

<h3>Jquery appendTo and prependTo Example - Websolutionstuff</h3>

<p>This is a paragraph.</p>

<button>Insert span element at the beginning of P tag</button>

</body>
</html>

 


You might also like :

Recommended Post
Featured Post
How To Implement Google Bar Chart In Vue Js
How To Implement Google Bar Ch...

In this tutorial, we will see how to implement google bar chart in vue js. In vue js perform bar chart tutorial we are u...

Read More

Jan-17-2022

How to Install Zoom in Ubuntu 22.04 using Terminal
How to Install Zoom in Ubuntu...

Greetings Ubuntu enthusiasts! If you're ready to dive into the world of video conferencing on your Ubuntu 22.04...

Read More

Jan-19-2024

How To Get Current Route In React JS
How To Get Current Route In Re...

As a web developer using React JS, I've come to appreciate the power and efficiency of this JavaScript library. Its...

Read More

Aug-11-2023

How To Get Current Date And Time In Node.js
How To Get Current Date And Ti...

In this example we will see how to get current date and time in Node.js application. In Node.js date and time are handle...

Read More

Sep-01-2021