Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any method/way in javascript to add a child node to list element dynamically?

Tags:

People also ask

Which JavaScript command is used to dynamically add new elements?

New elements can be dynamically created in JavaScript with the help of createElement() method. The attributes of the created element can be set using the setAttribute() method.

Which command is used to add a node to a child node?

The appendChild() method adds a child node to an existing node. The new node is added (appended) after any existing child nodes.

Which DOM method would you use to add a node element as a child to an existing HTML node?

The appendChild() method appends a node (element) as the last child of an element.

What property do you use to iterate through the list of an element's child nodes?

The childNodes property returns a collection (list) of an elements's child nodes.


If I have an unordered list like

<ul id="list">
<li>Helo World-1</li>
<li>Helo World-2</li>
<li>Helo World-3</li>
</ul>

I want to add a sublist item to it dynamically. Is there any method in javascript to do that. How could I do it. edit I need an item at next level, i.e. a sub list of Helo World that I mentioned in OP too, something like as under. One more issue here is that I need the items to be a permanent part of my code.

 <ul id="list">
    <li>Helo World-1</li>
    <li>Helo World-2</li>
    <li>Helo World-3</li>
       <ul>
         <li>One</li>
         <li>Two</li>
      </ul>
 </ul>