jQuery prependTo()

Introduction

Insert a <span> element at the beginning of each <p> element:

View in separate window

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

<button>test</button>

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

</body>
</html>

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

$(content).prependTo(selector)
Parameter
Optional
Description
content

Required.

the content to insert.
If content is an existing element, it will be moved from its current position.
selector
Required.
on which elements to prepend the content to



PreviousNext

Related