Skip to content Skip to sidebar Skip to footer

Hide Parent Div On Click Using Jquery

So I'm trying to write a super simple script that will allow a user to throw any link or button with the class .close inside of a div, and when that .close link is clicked, it auto

Solution 1:

Two problems:

  • live() doesn't exist in the version of jQuery in your fiddle (deprecated in 1.7, removed in 1.9)
  • fadeOut is a function (you were missing parens to execute it)

http://jsfiddle.net/KF7S6/

$('.close').on("click", function () {
    $(this).parents('div').fadeOut();
});

If you want it to work on dynamic elements, use this version:

$(document).on('click', '.close', function () {
    $(this).parents('div').fadeOut();
});

Solution 2:

http://jsfiddle.net/6Xyn4/4/

$('.close').click(function () {
    $(this).parent().fadeOut();
});

Recommended to use .click() now in place of deprecated .live()

Solution 3:

working demohttp://jsfiddle.net/gL9rw/

Issue was .live which is deprecated now.

If you keen: What's wrong with the jQuery live method?:)

code

$('.close').on("click", function () {
    $(this).parents('div').fadeOut();
});

Solution 4:

Try this, it should be fadeOut() not fadeOut

$('.close').click(function () {
  $(this).parents('div').fadeOut();
});

Solution 5:

.live() is dead. use .on() delegates. Also you missed something, check below

$('body').on("click", '.close', function () {
        $(this).parents().fadeOut();
    });

Fiddle

Post a Comment for "Hide Parent Div On Click Using Jquery"