count - using jQuery to add and remove textbox with unique IDs -


i wrote function add listitem text box , remove button, everytime add button clicked. i'm adding unique id each new element adding number. problem happens when try remove element, add one, number being duplicated. ex: add 4 li's , decide remove #3. click add new again new sequence 1,2,4,3,4 instead of 1,2,4,5,6. hope made sense.
here 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 doing research on stackoverflow, , found post performing check duplicate id's. new code works, have figure out how write "find last id , + 1, create new li new id. here 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 last id , + 1, add new listitem                      }else{                 $(inputacc).appendto(newtxtbxli);                 accomplishcount++;                 }              });          $('.remove').each(function() {             $(this).click(function() {                 count--;                 $(this).parent('li').remove();             });         });     });​ 

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

because of this, adding multiple identical click handlers .remove elements.

just assign new 1 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 alternative assigning new click handler every .remove created, use .live() or .delegate() instead.

   // call delegate on ul when page loads.    // idea have id on 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 .live() version this:

$('ul > li > .remove').live('click',function() {             //count--;             $(this).parent('li').remove();         }); 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -

PostgreSQL 9.x - pg_read_binary_file & inserting files into bytea -