jQuery Method How to - Use before() to add tag before








Question

We would like to know how to use before() to add tag before.

Answer


<!--from  w  w  w  .j a  va 2  s.c o  m-->
<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $("p").before("<b>Hello</b>");
        });
    </script>
  </head>
  <body>
         <b>b</b>
         <p>p</p>
  </body>
</html>

The code above is rendered as follows: