...

View Full Version : How to return false on an event handler defined using script in NS6



WA
08-03-2002, 11:48 PM
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:



<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.

nolachrymose
08-04-2002, 03:30 AM
Try using the stopPropagation() method of the event object:


function stopIt(evt) {
evt.stopPropagation();
} document.test.test2.addEventListener("keypress",stopIt,false);

Hope that helps!

Happy coding! :)

jkd
08-04-2002, 03:41 AM
Originally posted by nolachrymose
Try using the stopPropagation() method of the event object:


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.

WA
08-04-2002, 05:28 AM
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+...

jkd
08-04-2002, 05:50 AM
document.body.addEventListener('keypress', function(event) { if (event.target.tagName.toLowerCase() == 'input') event.stopPropagation() }, true);

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum