Jquery - Passing Variable Within A Function To Another Function
Solution 1:
drop the var
inside so the variable reference is not locally scoped to the click.
var add1;
$('#Button1').on('click', function(e) {
add1 = 'yes';
e.preventdefault();
})
$('#Button2').on('click', function(e) {
add1 = 'no';
e.preventdefault();
})
$('#DoneButton').on('click', function(e) {
submitLink = 'submit.php'
dataString = 'add=' + add1;
$.ajax({
type: "POST",
url: submitLink,
data: dataString,
cache: false,
success: function(data) {
/* My Success Notification Here */
}
});
e.preventdefault();
});
Solution 2:
Putting var add1
in the function declares it locally in that function. If you want to declare it outside, you have to add var add1;
outside the function, and then remove the var
s inside the function, ie `add1 = 'no';
Solution 3:
You need to define the variable in the shared scope so that all the functions can access it, in this case that's the global scope. Then assign a value to it inside the local scope of the function.
The simple part you were missing is that once you've declared it in the shared scope using var myVar
, you don't need to use the var
keyword again when assigning to it, because doing so creates a different variable only accessible in the local scope of the function, regardless of whether it has the same name.
Post a Comment for "Jquery - Passing Variable Within A Function To Another Function"