2

I'm new to jQuery and was wondering how I'd access $hidden outside of this function: $(this).hide(500, function ()

HTML

<div class="fl person"> 
    <input type="hidden" name="userSaved" value="1" /> 
    <img src="..." class="circle-mask" /> 
</div>

<div class="fl person"> 
    <input type="hidden" name="userSaved" value="2" /> 
    <img src="..." class="circle-mask" /> 
</div>

jQuery

<script>
$("div.person img").click(function () {    
    $(this).hide(500, function () {
        $(this).parent("div").empty();
        $(".main_page").appendTo("div.main_page").addClass("fl person"); 
        var saved_id_user_who_voted = $hidden = $(this).siblings('input');
    });

    /* attach a submit handler to the form */
    var saved_id_user_who_voted_val = "<?php echo $_SESSION['id']; ?>";

    /* stop form from submitting normally */
    event.preventDefault();

    /* Send the data using post and put the results in a div */
    $.ajax({
        url: "saveSavedUserToDatabase.php",
        type: "post",
        data: {saved_id_user_who_voted:saved_id_user_who_voted_val, saved_id_user_voted_on:saved_id_user_voted_on_val} 
    });
});
</script>

I'd like to be able to access saved_id_user_who_voted outside of the current function so that I can post it using .ajax - right now it's not in scope.

Any help is appreciated.

PaulEx10
  • 155
  • 2
  • 10
  • 1
    Pass it as a parameter to your function? – tymeJV Jul 18 '13 at 17:56
  • 1
    remove the `var` from `saved_id_user_who_voted` in the `hide` function. Then it will use the one that is in the outer scope, instead of creating a new one within the function scope. – taldonn Jul 18 '13 at 17:56
  • possible duplicate of [Accessing variables from other functions without using global variables](http://stackoverflow.com/questions/407048/accessing-variables-from-other-functions-without-using-global-variables) – Diodeus - James MacFarlane Jul 18 '13 at 17:58
  • actually, my suggestion won't work since the `hide` function is going to be called after 500 ms (when hide completes), when the ajax will have already executed. rather, you could just move the $.ajax into the `hide` function. – taldonn Jul 18 '13 at 18:12

1 Answers1

1

Execute your AJAX request in the hide callback. Or declare the variable outside the hide callback.

$("div.person img").click(function () {
    $(this).hide(500, function () {
        $(this).parent("div").empty();
        $(".main_page").appendTo("div.main_page").addClass("fl person");
        var saved_id_user_who_voted = $hidden = $(this).siblings('input');

        /* attach a submit handler to the form */
        var saved_id_user_who_voted_val = "<?php echo $_SESSION['id']; ?>";

        /* stop form from submitting normally */
        event.preventDefault();

        /* Send the data using post and put the results in a div */
        $.ajax({
            url: "saveSavedUserToDatabase.php",
            type: "post",
            data: {
                saved_id_user_who_voted: saved_id_user_who_voted_val,
                saved_id_user_voted_on: saved_id_user_voted_on_val
            }
        });
    });
});
sdespont
  • 13,915
  • 9
  • 56
  • 97