Results 1 to 3 of 3

Thread: Weird global variable behavior

  1. #1
    Join Date
    Aug 2009

    Weird global variable behavior

    Help. I'm working a project requiring a little client-side programming. Unfortunately my JavaScript is giving me strange results right out of the gate. The problem seems to be that the global variable 'boxChecked' is undefined inside the keyPressHandler function, or it reverts to the initialization value if I give it one. This code goes in an aspx file and I'm working in Visual Studio. Has anyone ever seen anything like this? I've tried declaring boxChecked without var and it gives the same results. All my JavaScript code is included below, and both functions are called when their associated events occur.

    HTML Code:
    <script type="text/javascript" language="javascript">
    // <!CDATA[
    var boxChecked;
    function onJavaCheckSwipe() {
        chkSwipe = document.getElementById('LoginView1_chkSwipe');
        if (chkSwipe.checked) {
            boxChecked = true;
        } else {
            boxChecked = false;
    // Prevent the "Enter" key from activating the currently highlighted button.
    function keyPressHandler(evt) {
        if (boxChecked) { alert("Box is checked"); }
        else { alert("Box is NOT checked"); }
        var evt = (evt) ? evt : ((event) ? event : null);
        var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
        if (evt.keyCode == 13)  { return false; }
    document.onkeypress = keyPressHandler;
    // ]>

  2. #2
    Join Date
    Jul 2008
    urbana, il
    where is onJavaCheckSwipe() called to set the variable in the first place?
    Create, Share, and Debug HTML pages and snippets with a cool new web app I helped create: pagedemos.com

  3. #3
    Join Date
    Aug 2009
    Here's the control:

    HTML Code:
    <asp:CheckBox ID="chkSwipe" name="chkSwipe"
                            onClick="javascript: onJavaCheckSwipe()"
                            AutoPostBack="True" TabIndex="1" />
    Now, on a hunch I turned AutoPostBack to "False" and my JavaScript worked as I expected! Now can someone explain in tiny words why turning off AutoPostBack would fix this problem?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 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