Add a DOM element between two children

I want to add a dom element between two child elements, especially just before the last one.

<div id='parent'> <div class='nav-option'></div> <div class='nav-option'></div> <div class='nav-option'></div> {{I want to insert something here}} <div class='nav-option'></div> </div> 

Now I am using the following code:

 $('#add-account').click(function(event){ $('#face').append('<div class="nav-select"></div>'); }); 

But alas, this adds an element to the end.

+4
source share
4 answers

Try the following:

 $('div.nav-option:last') .before('<div class="nav-select"></div>'); 

Check it out to find out more:

+2
source

jquery has .insertBefore and .insertAfter which do what you need.

0
source

Try

 $('.nav-option:last').before('<div class="nav-select"></div>'); 
0
source

Source: https://habr.com/ru/post/1346644/


All Articles