javascript - Choose <li> insertion place in list with jQuery according to id of siblings -


i have regular html ordered list:

<nav id="global_nav">     <ol class="menu">     <li id="section1">section1</li>     <li id="section2">section2     <ol>         <li id="subsection1">subsection1</li>             <li id="subsection2">subsection2</li>            <li id="subsection3">subsection3</li>       </ol>       </li>       <li id="section3" >item3</li>   </ol> </nav> 

and made small html form too:

<form>     <label for="nuevo">insert</label>     <input type="text" id="nuevo">     <label for="despues">after section</label>     <input type="text" id="despues">     <input type="button" id="btn" value="insert"> </form> 

the point able populate list dinamically using jquery. example, if in text field 1 type 'section4' , in text field 2 type 'section3', function add new <li>element text 'section4' right after existing 'section3'.

i have done this, , kind of works ok:

    $("#btn").click(function(){      var nuevo=$('#nuevo').val();     //var despues=$('#despues').val();     var nl = $('<li></li>').text(nuevo).attr('id', nuevo ).slidedown();      $(".menu").append(nl); 

but see, have commented out line grabbing value of second text field, because cannot understand how should make reference sibling item determine placement of new item. instance, if first item attach "section7" , goes appended bottom of list, how can attach "section6" later on right before "section7"?

what jquery functions need use in order going this? knew bit of pure javascript. trying understand cool things jquery in case finding in docs lot of functions apparently similar, quite confused.

thanks!

here's fiddle same. hope helps. http://jsfiddle.net/zy1f7ohc/5/

$("#btn").click(function(){      var nuevo=$('#nuevo').val();     var despues=$('#despues').val();       $('<li></li>').text(nuevo).attr('id', nuevo).insertafter("#"+despues);   }); 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -