Prepending a node in Jquery

I want to add a div before another div.

<div class = "abc">    
    <div class= "asd"> </div>
    <div class= "asd"> </div>
    <div class= "asd"> </div>
    <div class= "new"> </div>
</div>

I am trying to append a <p> add</p> before <div class="new"></div> but when i use $('.new').prepend('<p> </p>') the p tag is adding inside the div. How to overcome this issue

Answers 1

  • $('.new').before('<p> add</p>');
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div class = "abc">    
        <div class= "asd">1 </div>
        <div class= "asd">2 </div>
        <div class= "asd">3</div>
        <div class= "new">4 </div>
    </div>

    Use .before()

    Description: Insert content, specified by the parameter, before each element in the set of matched elements.


Related Articles