JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy
  • Contact Us

JQuery prepend() and prependTo() Example

May 13, 2014 by Krishna Srinivasan Leave a Comment

JQuery Prepend() Method

JQuery prepend() method is used to insert content to the beginning of each element inside matching of HTML elements. It inserts specified content as the first child of each element in the jQuery collection. With prepend() method, the selector expression preceding the method is the container into which the content is inserted. This method is similar to append() method, but only difference is that append() method inserts content to the end of selected elements.

JQuery Prepend() Syntax

[code lang=”xml”]
$(selector).prepend(content);
[/code]

It contains parameter called content which specifies content to be inserted at the beginning of each element in the set of matched elements.

JQuery Prepend() Example

[code lang=”xml”]
<!doctype html>
<head>
<title>JQuery Prepend Method</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<h2>JQuery Prepend Method Example</h2>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$(".myprepend").prepend("<strong>Div element is prepended…</strong>");
});
});
</script>
<body>
<button>Click to see prepend elements</button>
<div class="myprepend" style="color:red">
<p>Hello World!!!</p>
</div>
</body>
</html>
[/code]

  • As shown in the above program, we have used the code inside $(document).ready which is an event which fires up when document is ready. It will run once the page document object model is ready for JavaScript code to execute.
  • $(“button”).click(function()) line defines click event which occurs when button is clicked.
  • $(“.myprepend”).prepend(“Div element is prepended…“); statement indicates JS interpreter to prepend the content in quotes to div class elements.
  • When you run the script, click on the button which will appear on the browser, the line Div element is prepended… prepends before the line Hello World!!!.

JQuery Prepend() Demo

  • Run This Demo

When you run the above example, you would get the following output:

JQuery Prepend Method Example

JQuery PrependTo() Method

The prependTo() method also do the same task as prepend() method does. It inserts content to the beginning of the specified elements. The prepend() method precedes the content and with the prependTo() method, the content precedes the method as selector expression and inserted into the target container. The major difference between both methods is in the syntax, specifically in the placement of the content and target.

JQuery PrependTo() Syntax

[code lang=”xml”]
$(content).prependTo(selector);
[/code]

It contains parameter called selector which specifies on which elements to prepend the content to, in the set of matched elements.

JQuery PrependTo() Example

[code lang=”xml”]
<!doctype html>
<head>
<title>JQuery PrependTo Method</title>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
</head>
<h2>JQuery PrependTo Method Example</h2>
<script type="text/javascript">
$(document).ready(function(){
$("button").click(function(){
$("<strong>Div element is prepended…</strong>").prependTo(".myprepend");
});
});
</script>
<body>
<button>Click to see prepend elements</button>
<div class="myprepend" style="color:red">
</div>
<p>Hello World!!!</p>
</body>
</html>
[/code]

  • As shown in the above program, we have used the code inside $(document).ready which is an event which fires up when document is ready. It will run once the page document object model is ready for JavaScript code to execute.
  • $(“button”).click(function()) line defines click event which occurs when button is clicked.
  • $(“Div element is prepended…“).prependTo(“.myprepend”); statement indicates JS interpreter to prepend the content in quotes to div class elements.
  • When you run the script, click on the button which will appear on the browser, the line Div element is prepended… prepends before the line Hello World!!!.

JQuery PrependTo() Demo

  • Run This Demo

When you run the above example, you would get the following output:

JQuery PrependTo Method Example

Filed Under: jQuery Tagged With: JQuery DOM Manipulation

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved