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 5 of 5
  1. #1
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts

    How to return false on a event handler defined using script in NS6

    Does anyone know if it's possible to return false on a event handler that's attached using script in NS6? Somehow for most events, it doesn't seem to work- in other words, the default action is not cancelled. For example, I can't get the below code to disable the onkeypress event in a textbox in NS6:

    Code:
    <form name="test">
    <input type="text" name="test2">
    </form>
    
    <script>
    document.test.test2.onkeypress = function(){return false};
    </script>
    The code works as expected in IE4+, but it would seem the only way to get it to work in NS6 is to define the event handler as a HTML attribute (<input type="text" onkeypress="return false">). This defeats the purpose of what I'm working on right now.

    I've tried a couple of things to get the above to work, though it's starting to look more like a bug/limitation in NS6 (and NS4, for that matter).

    Any insight appreciated.
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Posts
    338
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Try using the stopPropagation() method of the event object:

    Code:
    function stopIt(evt) {
    evt.stopPropagation();
    } document.test.test2.addEventListener("keypress",stopIt,false);
    Hope that helps!

    Happy coding!

  • #3
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    Originally posted by nolachrymose
    Try using the stopPropagation() method of the event object:

    Code:
    function stopIt(evt) {
    evt.stopPropagation();
    } document.test.test2.addEventListener("keypress",stopIt,false);
    Hope that helps!

    Happy coding!
    stopPropagation() merely halts event flow - you'd have to execute it during the capturing phase before the key event i applied to the <input/>.

    preventDefault() does just what it says, and seems to be more appropriate.

  • #4
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts
    I've just tried both, though nither worked in preventing the onkeypress event in the form field. So it would seem there's just no way to return false on some events this way in NS4+...
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #5
    jkd
    jkd is offline
    Senior Coder jkd's Avatar
    Join Date
    May 2002
    Location
    metro DC
    Posts
    3,163
    Thanks
    1
    Thanked 18 Times in 18 Posts
    document.body.addEventListener('keypress', function(event) { if (event.target.tagName.toLowerCase() == 'input') event.stopPropagation() }, true);

    That prevents any input from reaching any <input/> tag.


  •  

    Posting Permissions

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