AppendTo and PrependTo Method in jQuery
<!DOCTYPE html>
<html lang="en">
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title>AppendTo and PrependTo Method in jQuery</title>
<style>
body {
font-family: arial
}
#box {
background: lightgreen;
padding: 10px;
border: 1px solid #000;
}
</style>
<body>
<h1>jQuery AppendTo & PrependTo Methods</h1>
<div id="box">
<h2>Test Box</h2>
<p>Lorem, ipsum <span>dolor sit amet</span> consectetur adipisicing elit. Harum odio iure voluptatibus
praesentium quae temporibus sed sunt dolorum reiciendis magni ea, corporis consectetur eum dolorem
asperiores molestias dicta repellat quos.</p>
</div>
<br>
<button id="appendBtn">AppendTo</button>
<button id="prependBtn">PrependTo</button>
<script src="js/jquery-3.7.1.min.js"></script>
<script>
//Main Difference between append() and appendTo() is that in append(), first "selector" comes and
//after "content" comes while in appendTo(), first "content" comes and after "selector" comes as
//shown below (same difference in prepend() and prependTo()):
$(document).ready(function () {
$("#appendBtn").click(function () {
$("<h3>This is AppendTo Heading</h3>").appendTo("#box");
});
$("#prependBtn").click(function () {
$("<h3>This is PrependTo Heading</h3><p>This is just text</p>").prependTo("#box");
});
});
</script>
</body>
</html>


Comments
Post a Comment