Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Location
    Brasília, Brazil
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts

    onchange focus() issue

    When I trigger the onchange event clicking on the other input field the focus() and select() methods (used alone or combined) in the validate() function works, but when I use the tab key to trigger the onchange event they does not work. Why? What can be done? Using FF 1.0.8

    PHP Code:
    <html>
    <
    script type="text/javascript">
    function 
    validate(obj) {
       if (
    obj.value 2) {
          
    alert('must be <= 2');
          
    obj.focus();
          
    obj.select();
          }
    }
    </script>
    <body>
    <input type="text" id="a" onchange="validate(this);" value="1">
    <input type="text" id="b" onchange="validate(this);" value="2">
    </body>
    </html> 
    Regards, Kakao

    Edit: Works in Opera 8.52

  • #2
    Regular Coder
    Join Date
    Mar 2006
    Location
    Brasília, Brazil
    Posts
    153
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I found the solution. The problem is that the onchange event is handled first and then when the event returns the tab key press is handled and the focus go to the next element. The solution:
    PHP Code:
    <html>
    <
    script type="text/javascript">
    function 
    validate(objvalue) {
       if (
    obj.value 2) {
          
    alert('must be <= 2 ' obj.id);
          
    setTimeout(function () { obj.focus() }, 50);
          
    obj.value value;
          
    obj.select();
          }
    }
    </script>
    <body>
    <input type="text" id="a" onchange="validate(this, 1);" value="1">
    <input type="text" id="b" onchange="validate(this, 2);" value="2">
    </body>
    </html> 


  •  

    Posting Permissions

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