jquery - How can you remove a dynamically created div with unique id? -
jquery - How can you remove a dynamically created div with unique id? -
i'm going post code that's relevant. if need whole code, allow me know.
anyway, have next code:
var deck = []; var c = 0; $('#add').click(function(){ var addtodeck = $('input[name=searchitem]').val(); addtodeck = addtodeck.tolowercase(); deck.push(addtodeck); $('#save').append('<div id="cardlist' + (c++) + '">' + database[deck[deck.length-1]].name + '</div>'); }); which dynamically adds divs unique id's each time button pressed (ex: cardlist1, cardlist2, etc). now, if user clicks on div particular id (say cardlist2), want remove list.
i tried this:
$(document).on('click','#save', function(){ $('#cardlist' + 'c').remove(); }); but doesn't work. think i'm in right direction though.
just re-iterate, card game deck building application... want create easier on user letting them remove card that's in deck clicking on div card id.
in sec code example, c not defined or not have value expect. should work though:
$('#save').on('click','[id^=cardlist]', function(){ $(this).remove(); }); it hear click events on elements having id starts cardlist , descendants of element #save (instead of listening clicks on #save). makes simpler because this refer element clicked , want remove.
you give elements additional class , utilize instead of attribute-starts-with selector:
// illustration $('<div />', { id: 'cardlist' + (c++), 'class': 'myclass', text: database[deck[deck.length-1]].name }).appendto('#save'); // somewhere else $('#save').on('click','.myclass', function(){ $(this).remove(); }); you should think whether need unique ids @ all.
jquery
Comments
Post a Comment