'Jquery - Grab Variable from Another Function
I have two functions on my page. I am trying to find a way to access a variable in function 1 inside function 2:
$(function() {
$('a[id=1]').live("click", function(event) {
event.preventDefault();
var Var1 = "something1";
});
});
$(function() {
$('a[id=2]').live("click", function(event) {
event.preventDefault();
var Var2 = event.var1;
});
});
So I am sure my above is incorrect but you can see how in function 2 I am trying to grab a variable from the first function.
Is there a way I can do this? I don't know why, but I keep thinking it has something to do with the event.var1 or the event functionality.
Solution 1:[1]
Each event are will be unique from each click, you can store your result in a global variable in the window object and take care of the order of the events
$(function() {
$('a[id=1]').on("click", function(event) {
event.preventDefault();
window.Var1 = "something1";
});
});
$(function() {
$('a[id=2]').on("click", function(event) {
event.preventDefault();
var Var2 = window.Var1;
});
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Said |
