oshirowanen
oshirowanen

Reputation: 15925

Detecting closest or parent div

I have the following script:

http://jsfiddle.net/oshirowanen/pALBV/

How do I make this more dynamic, so I do not have to manually specify the button clicks for each button if the number of buttons on screens is unknown as in the end result, the containers along with the container content will be database generated.

At the moment, I have only specified button_2 in jquery script.

Upvotes: 0

Views: 251

Answers (2)

gor
gor

Reputation: 11658

This works:

$(function() {
    $('input[type="button"]').click(function() {
        var container = $(this).parent().parent();
        container.fadeOut('slow', function() {
            container.remove();
        });
    });
});

Upvotes: 1

zack
zack

Reputation: 3198

try this:

$(function() {
    $(':button').click(function() {
        $(this).closest('.container').fadeOut('slow', function() {
            $(this).remove();
        });
    });
});

Upvotes: 1

Related Questions