The before() method inserts specified content in front of (before) the selected elements. To insert content after selected elements, use the after() method.
Syntax:
$(selector).before(content,function(index))
|
Example :
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$("p").before("<p>Hello world!</p>");
});
});
</script>
</head>
<body>
<button>click here to Insert content before each p element</button>
<p>This is a paragraph....</p>
<p>This is a paragraph......</p>
</body>
</html>
|