/    Sign up×
Community /Pin to ProfileBookmark

Can’t access global variable outside of event handler

Hi I’m trying to access a global variable that I set inside an event handler. On my form I just click
inside a input box the problem code is basically like this

[CODE]
var dateSource;
jQuery(“body”).click(function (event) {
dateSource = event.target.id;
});

alert(dateSource);[/CODE]

The thing is if I run this, dateSource always comes out empty/blank; It never picks up the id of the inputbox

Thank you

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@TcobbFeb 05.2014 — You don't have the full code here--but that which is shown runs the alert BEFORE there is any chance for a user to click on the body and actually set the value of 'dateSource' to anything other than 'undefined.' And if the user does click, thereby setting the value, there is nothing to invoke the alert function.
×

Success!

Help @IceAngel spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...