www.webdeveloper.com
Results 1 to 2 of 2

Thread: Can't access global variable outside of event handler

  1. #1
    Join Date
    Nov 2010
    Posts
    1

    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);
    The thing is if I run this, dateSource always comes out empty/blank; It never picks up the id of the inputbox

    Thank you

  2. #2
    Join Date
    Mar 2009
    Posts
    467
    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.

Thread Information

Users Browsing this Thread

There are currently 3 users browsing this thread. (0 members and 3 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles