www.webdeveloper.com
Results 1 to 3 of 3

Thread: Weird global variable behavior

  1. #1
    Join Date
    Aug 2009
    Posts
    2

    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;
    // ]>
    </script>

  2. #2
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    where is onJavaCheckSwipe() called to set the variable in the first place?

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

    HTML Code:
    <asp:CheckBox ID="chkSwipe" name="chkSwipe"
                            oncheckedchanged="onCheckSwipe" 
                            onClick="javascript: onJavaCheckSwipe()"
                            runat="server" 
                            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



Recent Articles