Table of Contents
In this post, Â we are going to see jQuery after and insertAfter methods.
Live demo:
Jquery after and insertAfter example example on jsbin.com
Both do the same task, insert text or html after every selected elements but syntax is quite different.
Syntax for after():
1 2 3 4 5 |
$("selectors").after("element to be inserted") ; Example : $("div").after(" |
inserting using after
1 2 3 |
"); |
Syntax for insertAfter() :
1 2 3 4 5 |
$("element to be inserted") .insertAfter("div"); Example: $(" |
inserting using insertAfter
1 2 3 |
").insertAfter("div"); |
Let’s understand with the help of example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<title>Jquery after and insertAfter example example </title> <script src="http://code.jquery.com/jquery-2.2.3.min.js"></script> <h2>Jquery after and insertAfter example example</h2> <style> .red{ border:3px dotted red; color:red; font-family: arial narrow; } .blue{ border:3px dotted red; color:blue; font-family: arial narrow; } </style> <script type="text/javascript"> $(document).ready(function(){ $("#afterButton").click(function(){ $("div").after("<p class='red'>inserting using after"); }); $("#insertAfterButton").click(function(){ $("<p class='blue'>inserting using insertAfter").insertAfter("div"); }); $("#reset").click(function(){ location.reload(); }); }); </script> <button id="afterButton">Using after</button> <button id="insertAfterButton">Using insertAfter</button> <button id="reset">Reset</button> <div> Hello world from java2blog!!!! </div> <div> Welcome to JQuery. </div> |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.