views:

858

answers:

1

hello to all, im trying to use jquery in order to make a button remove its parent div.

my markup:

<div class="web_store_fields" id="web_store_input1">
<p>
<label for="web_store_address[]"  >Store Address : </label>        
<input name="web_store_address[]" class="web_store_info"  type="text" value="http://www." size="35"/>
<input class="button_remove_web_store" type="button" value="Remove"  />
</div>

jquery div removal code:

$('.button_remove_web_store').click(function() {
$(this).parents("div:first").remove();
});

this works ok for the div that's in the html when the page loads but not for div created dynamically by user (using this jquery code):

$('#button_add_web_store').click(function() {
var num = $('.web_store_fields').length;
var newNum  = new Number(num + 1);      
var newElem = $('#web_store_input' + num).clone().attr('id', 'web_store_input' + newNum);
$('#web_store_input' + num).after(newElem);
});

just to be clear, the dynamic creation is working fine, the problem is removing those divs.

any tips would be highly appreciated

+2  A: 

Use a live handler on the class so that any new buttons that get added later will still work.

$('.button_remove_web_store').live('click',function() {
    $(this).parents("div:first").remove();
});
tvanfosson
thank you so much, it's working!i'm a jquery newbie and apparently there's a lot i don't know..
samoyed