Tim Maxey .NET Technology Blog & Resources

JavaScript remove list items dynamically

During work on a project, I cannot reveal yet, I came across the issue of removing a CSS marked (looks like a FaceBook  list item ( li tag )), dynamically in html with JavaScript .

Basically the list items are added dynamically, much like FaceBook's friends when send a message. The list items are CSS'd and have the little "X" to remove. But how to remove?

This adds the list item:

var container=$('ulcontainer');
container.update(container.innerHTML+"<li class=fbclass id="+item.TitleValue+" onclick=removeitem('"+item.TitleValue+"')>"+item.TitleValue+"<img class=p src=images/delete.gif /></li>");

Basically notice the onclick in the li tag, removeitem...

function removeitem(o) {
     $(o).remove();
}

That's it! After hours of trying to figure it out, that was it!!! All the other stuff has to deal with other things, but in short, you use the $ or document.getelementByID and find the id, in my case I made each list item ( li tag) have id of the username and the removeitem function "finds" the control and .remove() s it, worked like a champ...
                                         
by Tim Maxey, Life Coach, Internet GURU


Feedback

No comments posted yet.


Post a comment





 

Please add 3 and 5 and type the answer here: