Welcome Guest! To access all forums & features, please register an account or sign-in. → Why register?



Append using Javascript or jQuery?


2 replies to this topic - - - - -

#1 Jose_49

    Neowinian Senior

  • 2,999 posts
  • Joined: 30-July 09

Posted 05 May 2012 - 17:20

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....


#2 Tjcool007

    Neowinian²

  • 103 posts
  • Joined: 27-April 11
  • Location: Belgium
  • OS: Windows 7 Pro x64, Ubuntu 13.04

Posted 05 May 2012 - 19:03

jQuery
$("#division > ul").append("<li>text here</li>");

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);​


#3 OP Jose_49

    Neowinian Senior

  • 2,999 posts
  • Joined: 30-July 09

Posted 05 May 2012 - 20:29

Nice! Thanks Tjcool007! :D