Mark Henry
Mark Henry

Reputation: 2699

Fadeout div after executing script with jQuery

With this function I want to submit data (res_id) to a script and fadeout the div by it's id (using variable pos), the script is executed but the div won't fade out. Why is that?

$('.remove_resort').click(function() {
    pos = $(this).attr("id");
    rem_res();
});

function rem_res() {
    $.get("/snowreport/request/remove.php", {
        res_id: pos
    }, function(data1) {
        pos.fadeOut("slow");
    });
}

Upvotes: 0

Views: 119

Answers (3)

Chris
Chris

Reputation: 10338

You're storing pos = $(this).attr("id"), which is a string, not a jQuery object. What your original code is doing is trying to call "somestring".fadeOut(), which won't work. If you keep a reference to the jQuery object for the element and call fadeOut on that, it should fade out.


Let's rewrite this code so that it does what you're after, is a little cleaner and more obvious:

$('.remove_resort').click(function() {
    rem_res(this);
});

function rem_res(element) {
    var $element = $(element),
        id = element.id;

    $.get("/snowreport/request/remove.php", { res_id: id }, function(data) {
        $element.fadeOut("slow");
    });
}

Upvotes: 0

karim79
karim79

Reputation: 342635

pos contains the ID string, so you need $("#" + pos).fadeOut("slow")

Upvotes: 1

Madara's Ghost
Madara's Ghost

Reputation: 174957

pos is outscoped. You should pass it as an argument to the function.

$('.remove_resort').click(function() {
    pos = $(this).attr("id");
    rem_res(pos);
});

function rem_res(pos) {
    $.get("/snowreport/request/remove.php", {
        res_id: pos
    }, function(data1) {
        pos.fadeOut("slow");
    });
}

Upvotes: 0

Related Questions