prependTo() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:prependTo

Description

The prependTo() method inserts HTML elements at the beginning of the selected elements.

For an existing element, it will be moved from its current position, and inserted at the beginning of the selected elements.

Syntax

Parameter Require Description
content Required. HTML tags content to insert.
selector Required. which elements to prepend the content to

The following code shows how to Insert a <span> element at the beginning of each <p> element:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("button").click(function(){
        $("<span>Hello World! </span>").prependTo("p");
    });//  w  w w  . j av  a 2  s.c  o  m
});
</script>
</head>
<body>

<button>Insert span element at the beginning of each p element</button>

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

</body>
</html>

Related Tutorials