append() and prepend()
Inserts content at the END / BEGINNING of the selected HTML elements.
Examples
$("p").append("Some appended text.");
$("p").prepend("Some prepended text.");
after() and before()
Inserts content AFTER / BEFORE the selected HTML elements.
Examples
$("img").after("Some text after");
$("img").before("Some text before");
Adding several new elements
All the above methods can take an infinite number of new elements as parameters.
The new elements can be generated with text/HTML, jQuery or JavaScript code and DOM elements.
Example 1. Adding new elements with append()
function appendText() {
var txt1="<p>Text.</p>"; // Create element with HTML
var txt2=$("<p></p>").text("Text."); // Create with jQuery
var txt3=document.createElement("p"); // Create with DOM
txt3.innerHTML="Text.";
$("p").append(txt1,txt2,txt3); // Append the new elements
}
Example 2. Adding new elements with after()
function afterText() {
var txt1="<b>I </b>"; // Create element with HTML
var txt2=$("<i></i>").text("love "); // Create with jQuery
var txt3=document.createElement("big"); // Create with DOM
txt3.innerHTML="jQuery!";
$("img").after(txt1,txt2,txt3); // Insert new elements after img
}
|
Saturday, 26 March 2016
How to add new HTML contents ?
Labels:
jQuery
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment
Note: only a member of this blog may post a comment.