Hello Redaers,
jquery prepend() is the jquery method which is used to append or add the content or jquery elements at the BEGINING of the each selected or matched elements in jquery.
Syntax :
$(selector).prepend(content,function(index,html))
Parameters :
content : it is the required parameter which specifies the content to insert at the begining of the matched elements (HTML elements, jQuery objects, DOM elements).
function : it is optional parameter and this function returns a HTML string when we insert the content at the begining of the matched elements.
Code Example :
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("#btn1").click(function(){
$("p").prepend("<b>Prepended text</b>. ");
});
$("#btn2").click(function(){
$("ol").prepend("<li>Prepended item</li>");
});
});
</script>
</head>
<body>
<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<ol>
<li>List item 1</li>
<li>List item 2</li>
<li>List item 3</li>
</ol>
<button id="btn1">Prepend text</button>
<button id="btn2">Prepend list item</button>
</body>
</html>
In the above code, we use the prepend() method with selector p pargraph and ol ordered list and on click button add or append the content at the begining of the matched elements in jquery.
0 Comment(s)