views:

59

answers:

3

I wrote a function to add listitem with a text box and a remove button, everytime the add button was clicked. i'm also adding a unique id to each new element by adding a number. the problem happens when i try to remove an element, then add another one, sometimes that number is being duplicated. ex: I add 4 li's and decide to remove #3. Then click add new again the new sequence is 1,2,4,3,4 instead of 1,2,4,5,6. I hope that made sense.
here is my javascript

var count = 2;
$('#add').click(function() {
    var newTxtBxLi = $('<li></li>').attr('id', 'newli' + count).appendTo('ul');
    var input = $('<input type="text" id="newinput' + count + '" name="newinput' + count + '" /><input type="button"  id="remove' + count + '" class="remove" value="">');

    $(input).appendTo(newTxtBxLi);

    count++;

    $('.remove').each(function() {
        $(this).click(function() {
            //count--;
            $(this).parent('li').remove();
        });
    });
});​

Thanks, in advance

//update so i was doing some research on stackoverflow, and found a post about performing a check for duplicate id's. the new code works, but now i have to figure out how to write "find the last id and + 1, then create new li with this new id. here is my updated code:

    $('#add').click(function() { 
var count = $('ul > li').length + 1;
        var newTxtBxLi = $('<li></li>').attr('id', 'newli' + count).appendTo('ul');
        var input = $('<input type="text" id="newinput' + count + '" name="newinput' + count + '" /><input type="button"  id="remove' + count + '" class="remove" value="">');

        $('ul > li[id]').each(function(){
            var ids = $('[id='+this.id+']');
            if(ids.length>1 && ids[0]==this){
                //console.warn('Multiple IDs #'+this.id);
                //find the last id and + 1, then add new listitem    

                }else{
                $(inputAcc).appendTo(newTxtBxLi);
                accomplishCount++;
                }

            });

        $('.remove').each(function() {
            $(this).click(function() {
                count--;
                $(this).parent('li').remove();
            });
        });
    });​
A: 

I believe this is what you are trying to accomplish.

http://www.mkyong.com/jquery/how-to-add-remove-textbox-dynamically-with-jquery/

Manaf Abu.Rous
yea, that's the exact one i modeled mine after. only thing i did differently was add the remove button to each new element. that way the user would have the option of removing any new li instead of only being able to remove the last one. that's where i'm having the problem
Gerald
+1  A: 

You shouldn't be re-assigning the click() handler to all the .remove elements on the page every time you click #add.

Because of this, you are adding multiple identical click handlers to the .remove elements.

Just assign it to the new one you created.

var count = 2;
$('#add').click(function() {
    var newTxtBxLi = $('<li></li>').attr('id', 'newli' + count).appendTo('ul');

    var input = $('<input type="text" id="newinput' + count + '" name="newinput' + count + '" /><input type="button"  id="remove' + count + '" class="remove" value="">');

    input.find('.remove').click(function() {
            //count--;
            $(this).parent('li').remove();
        });

    input.appendTo(newTxtBxLi);

    count++;
});​

As an alternative to assigning a new click handler to every .remove that is created, you could use .live() or .delegate() instead.

   // Call delegate on the UL when the page loads.
   // Probably a good idea to have an ID on the UL
$('ul').delegate('.remove','click',function() {
            //count--;
            $(this).parent('li').remove();
        });

var count = 2;
$('#add').click(function() {
    var newTxtBxLi = $('<li></li>').attr('id', 'newli' + count).appendTo('ul');

    var input = $('<input type="text" id="newinput' + count + '" name="newinput' + count + '" /><input type="button"  id="remove' + count + '" class="remove" value="">');

    input.appendTo(newTxtBxLi);

    count++;
});​

Or the .live() version would look like this:

$('ul > li > .remove').live('click',function() {
            //count--;
            $(this).parent('li').remove();
        });
patrick dw
Thanks patrick! that's awesome!
Gerald
@Gerald - Did this happen to fix the repeating IDs issue? It didn't seem entirely related, but wasn't sure if it perhaps caused some unexpected behavior.
patrick dw
no I'm still trying to figure that out
Gerald
@Gerald - I see you have `count--` commented out. Is it commented out in the code you're running as well? I'd imagine you wouldn't want to decrement.
patrick dw
i tried it both ways, leaving it in and taking it out, and both times i got the same results. the last number was being duplicated. I think a check has to be performed before the .appendTo action is called. that's what i'm trying to figure out now
Gerald
@Gerald - That's strange. It should be very simple. Seems like some other code is decrementing it, or it is not getting increment sometimes. Maybe try placing the `count++` at the beginning of the click handler for `#add`.
patrick dw
A: 

What about instead of trying to keep track of a manual count you instead made a count a function of how many li elements are on your page? i.e.

$('#add').click(function(){
    count = $('li').length;
    var newTxtBxLi = $('<li></li>').attr('id', 'newli' + count).appendTo('ul');
    var input = $('<input type="text" id="newinput'+ count +'" name="newinput'+ count +'" /><input type="button"  id="remove'+ count +'" class="remove" value="">');

    $(input).appendTo(newTxtBxLi);

    $('.remove').each(function(){
        $(this).click(function(){
            $(this).parent('li').remove();
        });
    });
});

I'm not sure exactly why you wanted the sequence to go 1,2,4,5,6 though, as simply having each one have a unique id should suffice. But perhaps that was important?

Tim Visher
you're right, the sequence is not important. Just as long as each one is unique. That being said, i used your solution and i'm still having the issue where the id's are being repeated.
Gerald
This doesn't count the ones that were removed, so you could still have the same issue.
patrick dw
boo… :) Sorry 'bout that.
Tim Visher