JQuery Before()

It is used to insert the specified content before the selected elements.

Both jQuery before() and insertBefore() methods are doing the same task, add a text or html content before the matched elements. The major difference is in the syntax.

before() Syntax:

$(selector).before('content')

insertBefore() Syntax:

$("<div class='box'>content</div>).insertBefore('.box');

before method example

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery before()</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function() {
  $("#button").click(function() {
     $("p").before("<strong>I'm coming from before() method.</strong>");
  });
});
</script>
</head>
<body>
 <p>This is first paragraph</p>
 <button style="margin:0 0 10px 0;background:#e8e8e8;" class="btn" id="button">Click here</button><br />
 </body>
</html>