Hello Readers,
jquery before() method is the opposite of jquery after() method and it is used to insert the given content before the specified of each selected content or elements in jquery.
Syntax :
$(selector).before(content,function(index))
content : this is required parameter which specifies the content to insert.
function : this function is used to return the HTML after the content insert.
Code Example :
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").before("<p>Hello world!</p>");
});
});
</script>
</head>
<body>
<button>Insert content before each p element</button>
<p>This is a paragraph.</p>
<p>This is a paragraph.</p>
</body>
</html>
In the above code use the before() method and insert the HTML content before the each
tag in jquery.
0 Comment(s)