• 0

Append using Javascript or jQuery?


Question

Guys I need help using jQuery or javascript. I'm trying to append a child <li> on a <ul> which is a child of a div.

jQuery API page is not working so I can't get reference.

Can you help me?

Example:

<div id="division">

<ul class="overview">

<li>Element #1 </li>

<li>Element #2 </li>

<li>Element #3 </li>

<li> An Element I would like to append</li>

</ul>

</div>

Thanks :p.

I've been trying to do this:

var caca = document.createElement('li');

document.getElementById('division').appendChild(caca);

But haven't get it....

Link to comment
https://www.neowin.net/forum/topic/1075241-append-using-javascript-or-jquery/
Share on other sites

2 answers to this question

Recommended Posts

  • 0

jQuery

$("#division &gt; ul").append("&lt;li&gt;text here&lt;/li&gt;");

There's other possibilities for the selector, this is just one way to do it.

Plain JavaScript would be something like this:

//Create list item
var item = document.createElement('li');

//Put text in it
item.appendChild(document.createTextNode('text here'));

//Get the first ul (index 0) from the element 'division'
var list = document.getElementById('division').getElementsByTagName('ul')[0];

//Add it to the list
?list.appendChild(item);?

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.