jQuery Inserts Elements
jQuery inserts elements dynamically inside the existing HTML document. These jQuery methods are: append()
, prepend()
, after()
, before()
.
jQuery inserts elements dynamically inside the existing HTML document. These jQuery methods are: append()
, prepend()
, after()
, before()
.
jQuery append() is used to insert content at the end of the selected elements.
$("p").append("Step by step tutorial.");
It is used to insert content at the beginning of the selected elements.
$("p").prepend("Learn Bootstrap 5");
jQuery append() and prepend() method also takes multiple argument as a input.
Let us understand it with the help of an example
Source Code
$(document).ready(function(){
$(".btn-multi-insert").click(function(){
var mainHeading = "<h2>Learn Jquery</h2>";
var pargarphText = document.createElement("p");
pargarphText.innerHTML = "<h3>Follow step by step process.</h3>";
$("body").append(mainHeading,pargarphText);
});
});
jQuery before() Method is very helpful to insert content before the selected HTML elements.
Source Code
$(document).ready(function () {
$(".btn-multi-insert").click(function () {
$(".center-element").before("<h1>Learn Jquery</h1>");
});
});
jQuery after() method is useful for inserting content after the selected HTML elements.
Source Code
$(document).ready(function () {
$(".btn-multi-insert").click(function () {
$(".center-element").after("<h3>To make interactive application.</h3>");
});
});
These methods also take multiple arguments as input. Let us understand it with the help of an example.
Source Code
$(document).ready(function () {
$(".btn-multi-insert").click(function () {
var Headingtext = "<h2>Learn jQuery</h2>";
var nextHeadingText = "<h2>step by step</h2>";
var paragraphText = document.createElement("p");
paragraphText.innerHTML = "<b>t o make an interactive application.<b>";
$("p").before(Headingtext, nextHeadingText, paragraphText);
});
});