How do I append a child to all the nodes with the specified classname using pure javascript

限于喜欢 提交于 2019-12-02 09:11:14

It should be

var menuheader = document.createElement("li");// creates main menu to which below submenu should be added. 
var submenus=document.getElementsByClassName("subMenu"); //gives an array so you cannot append child to that.

   for(int i=0;i<submenus.length;i++){
   menuheader.appendChild(submenus[i]);

  }

From your code it seems that you are doing reverse that is adding mainmenu to submenu which again.

  document.createElement("li"); //create a main menu say Services

Services

So I need to add submenu to above element so that it should look like

Services

->Non billable Service

->I.T service

->Billable Service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!