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
$(selector).prepend(content);
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
<!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>
- 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
When you run the above example, you would get the following output:
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
$(content).prependTo(selector);
It contains parameter called selector which specifies on which elements to prepend the content to, in the set of matched elements.
JQuery PrependTo() Example
<!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>
- 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
When you run the above example, you would get the following output: