arrays - Append last and first element pure javascript? -


i have menu in html

    <ul class="main-menu menu-bees" id="menu-main-menu" data-menu-width="1112">         <!-- rowtpl -->         <li class="menu-item bees-start active" id="menu-item-0">             <a href="#">                 menu 1                 <div class="canvas_wrapper">                     <canvas class="menu_dashed" height="64" width="501"></canvas>                 </div>             </a>             <span class="button_open"></span>          </li>         <!-- rowtpl -->         <li class="menu-item" id="menu-item-1">             <a href="#">                 menu 2                 <div class="canvas_wrapper">                     <canvas class="menu_dashed" height="64" width="501"></canvas>                 </div>             </a>             <span class="button_open"></span>          </li>         <!-- rowtpl -->         <li class="menu-item" id="menu-item-2">             <a href="#">                 menu 3                 <div class="canvas_wrapper">                     <canvas class="menu_dashed" height="64" width="501"></canvas>                 </div>             </a>             <span class="button_open"></span>         </li>        </ul> 

what need append html, id# menu-item-xxx counts , add number of elements in array starting 0. can not use jquery need pure js doing this, , need html in first li > a , in last, dont know how many has append last. when append html has this

    <ul class="main-menu menu-bees" id="menu-main-menu" data-menu-width="1112">         <!-- rowtpl -->         <li class="menu-item bees-start active" id="menu-item-0">             <a href="#">               **<p>first</p>**                 menu 1                 <div class="canvas_wrapper">                     <canvas class="menu_dashed" height="64" width="501"></canvas>                 </div>             </a>             <span class="button_open"></span>          </li>         <!-- rowtpl -->         <li class="menu-item" id="menu-item-1">             <a href="#">                 menu 2                 <div class="canvas_wrapper">                     <canvas class="menu_dashed" height="64" width="501"></canvas>                 </div>             </a>             <span class="button_open"></span>          </li>         <!-- rowtpl -->         <li class="menu-item" id="menu-item-2">             <a href="#">               **<p>last</p>**                 menu 3                 <div class="canvas_wrapper">                     <canvas class="menu_dashed" height="64" width="501"></canvas>                 </div>             </a>             <span class="button_open"></span>         </li>        </ul> 

it means need add :

first

first li > a
,

last

last li >

and pure js, idea, count again dont know how many li :(

if know how write js make code, dont know start :(

you can use queryselectorall() select li elements , use for loop check if last or first li element , add html insertadjacenthtml.

var li = document.queryselectorall('#menu-main-menu li');  (var = 0; < li.length; i++) {    if (i == 0) {      li[i].queryselector('a').insertadjacenthtml('afterbegin', '<p>first</p>');    }      if (i == (li.length - 1)) {      li[i].queryselector('a').insertadjacenthtml('afterbegin', '<p>last</p>');    }  }
<ul class="main-menu menu-bees" id="menu-main-menu" data-menu-width="1112">    <!-- rowtpl -->    <li class="menu-item bees-start active" id="menu-item-0">      <a href="#">                  menu 1                  <div class="canvas_wrapper">                      <canvas class="menu_dashed" height="64" width="501"></canvas>                  </div>              </a>      <span class="button_open"></span>      </li>    <!-- rowtpl -->    <li class="menu-item" id="menu-item-1">      <a href="#">                  menu 2                  <div class="canvas_wrapper">                      <canvas class="menu_dashed" height="64" width="501"></canvas>                  </div>              </a>      <span class="button_open"></span>      </li>    <!-- rowtpl -->    <li class="menu-item" id="menu-item-2">      <a href="#">                  menu 3                  <div class="canvas_wrapper">                      <canvas class="menu_dashed" height="64" width="501"></canvas>                  </div>              </a>      <span class="button_open"></span>    </li>  </ul>


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -