Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

listitem - Remove - dynamically

I have created the list based on the AJAX result. Basically dynamically add the items into the list. But In the second time ajax calling,the list item append to the previous items. I want to reload the list newly. It should contains results of second ajax calling.

I want to remove the all the list items alone.Not a list from the page.

Any one help me.

Code for dynamically add the list value:

var parent = document.getElementById('listview');
var listItem = document.createElement('li');
listItem.setAttribute('id','listitem_'+i);
listItem.innerHTML = "one";
parent.appendChild(listItem);
$(parent).listview("refresh"); 
like image 868
Finder Avatar asked Jan 10 '11 14:01

Finder


3 Answers

$('#listview').children().remove('li');

this should do the trick to clear the list.

P.S. If you intend to use jQuery, then use it.

like image 85
Rumplin Avatar answered Nov 05 '22 19:11

Rumplin


If you want to remove everything, then why not use jQuery to do so?

$('#listview').empty();
like image 43
naugtur Avatar answered Nov 05 '22 17:11

naugtur


This will help you create a iphone like swipe delete. This is in reference to http://forum.jquery.com/topic/adding-an-iphone-style-swipe-to-delete-button-to-a-listview

EXERCISEDESCRIPTION.swipeDelete = function(exerciseSetsListview, callback) {

    var listViewJq = '#'+exerciseSetsListview;
    $(listViewJq).children().each(function(){
        var child = $(this);
        var childId = child.attr('id');
        var splitId = childId.split("_");
        var childIdVar = '#'+childId;
        var childIdBtnVar = splitId[0]+'_button_'+splitId[1];
        var childIdBtnVarJq = '#'+childIdBtnVar;

        $(childIdVar).bind('swiperight', function() {
              $(childIdVar).prepend('<a href="#" id="'+childIdBtnVar+'" class="aSwipeBtn" data-theme="b" data-inline="true" data-role="button">Delete</a>');
              $(childIdBtnVarJq).button();
              $(childIdVar).unbind('swiperight');
              $(childIdBtnVarJq).bind('click tap', function() { 
                  $(childIdVar).remove();
                  var splitButtonId = childIdBtnVarJq.split("_");
                  callback(splitButtonId[2]);
              });
            });         

    });

};

usage:

EXERCISEDESCRIPTION.swipeDelete('exerciseSetsListview',
    function(e) {
        EXERCISEDESCRIPTION.setsObj.splice(e,1);
        EXERCISEDESCRIPTION.repopulateSets();
    }); 
like image 42
mayan Avatar answered Nov 05 '22 19:11

mayan