Closed Bug 295246 Opened 19 years ago Closed 19 years ago

Using addEventListener() keypress events are not working like expected

Categories

(Core :: DOM: Events, defect)

x86
Windows XP
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: alex, Unassigned)

Details

(Keywords: testcase)

Attachments

(1 file)

User-Agent:       Mozilla/5.0 (Windows; U; Windows NT 5.1; es-AR; rv:1.7.8) Gecko/20050511 Firefox/1.0.4
Build Identifier: Mozilla/5.0 (Windows; U; Windows NT 5.1; es-AR; rv:1.7.8) Gecko/20050511 Firefox/1.0.4

There is a diferent behavior when adding events throught addEventListener and
setting directly using element.onkeypress

Reproducible: Always

Steps to Reproduce:
Create a test page:

[page]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>What's wrong?</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>

<input type="text" id="foo" />
<input type="text" id="bar" />

<script type="text/javascript">
function addEvent(el, evname, func) {
	if (el.attachEvent) { // IE
		el.attachEvent("on" + evname, func);
	} else if (el.addEventListener) { // Gecko / W3C
		el.addEventListener(evname, func, false);
	} else {
		el["on" + evname] = func;
	}
};
function kp_handler(){
	return false;
}
addEvent(document.getElementById('foo'), 'keypress', kp_handler); [*a]
document.getElementById('bar').onkeypress = kp_handler;           [*b]
</script>
</body>
</html>
[/html]
Actual Results:  
In both cases the event handler function (kp_handler) gets called:

[*a] No action is taken, I can write anything in 'foo'

[*b] I can't write in 'bar' // Right

Expected Results:  
[*a] It shouldn't let write in 'foo'

[*b] It's allright
Attached file testcase
I added an alert("keypress") to the keyhandler().
1st input: type ONE character, confirm alert, and see character in the input.
2nd input: type ONE character, confirm alert, and don´t see character.

1st input uses addeventListener() and that just adds an eventlistener.
2nd input replaces the native keyhandler with a new one.

Is this function working differently in other browsers?

Keywords: testcase
You need to call event.preventDefault() to prevent the default action in an
event listener. The returning false thing only does something for attribute
handlers.
(In reply to comment #3)
> You need to call event.preventDefault() to prevent the default action in an
> event listener. The returning false thing only does something for attribute
> handlers.
> 

Thanks for the comment.
invalid then as far as i understand this, if you disagree, reopen.
Status: UNCONFIRMED → RESOLVED
Closed: 19 years ago
Resolution: --- → INVALID
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: