Skip to content Skip to sidebar Skip to footer

Input Button OnClick Remove Closest Div

Erm, why this don`t work, iv used similar code like this for my site many times..butnow dont work.. HTML
a

Solution 1:

How about adding a class to the button element and bind a handler to the button

<div>
   <span>a</span>
   <input type='hidden' /> 
   <input type='button' class'removeDiv' />

Code:

$(function () {
    $('.removeDiv').click (function () { 
         $(this).parent().remove();
         // As I see the input is direct child of the div
    });
});

My divs are created from clientside and serverside too. Its much easy to add onclick function instead to unbind-rebind all on new item, no?

In such cases you can use delegated events. See below,

$(function () {
    //Replace document with any closest container that is available on load.
    $(document).on('click', '.removeDiv', function () { 
         $(this).parent().remove();
         // As I see the input is direct child of the div
    });
});

Solution 2:

This should work!

HTML

<div><span>a</span><input type='hidden' /> <input type='button' onClick='Remove(this)' />

JQuery

function Remove(obj){
    $(obj).closest('div').remove();
}

Post a Comment for "Input Button OnClick Remove Closest Div"