Closed Bug 68909 Opened 25 years ago Closed 25 years ago

event.preventDefault() doesn't cancel click handler of INPUT TYPE="button" element

Categories

(Core :: DOM: Events, defect)

x86
Other
defect
Not set
normal

Tracking

()

VERIFIED INVALID

People

(Reporter: martin.honnen, Assigned: joki)

Details

(Keywords: dom2)

Attachments

(1 file)

I try to capture click events on buttons and cancel them with calling event.preventDefault() The onclick handler of the button nevertheless fires. <HTML> <HEAD> <SCRIPT> if (document.addEventListener) document.addEventListener('click', handleClick, true); function handleClick (evt) { if (document.getElementById) { var target = evt.target; if (target.nodeType == 3) target = target.parentNode; document.getElementById('output').innerHTML += evt.type + ' for ' + target + ' with name ' + target.name + ' in phase ' + evt.eventPhase + '; is cancelable: ' + evt.cancelable + '<BR>'; if (target.name == 'button2') { document.getElementById('output').innerHTML += 'trying to cancel event<BR>'; evt.preventDefault(); return false; } else return true; } } </SCRIPT> </HEAD> <BODY> <FORM NAME="formName"> <INPUT TYPE="button" NAME="button1" VALUE="Kibology" ONCLICK="alert('Kibology');" > <INPUT TYPE="button" NAME="button2" VALUE="Xibology" ONCLICK="alert('Kibology');" > </FORM> <DIV ID="output"></DIV> </BODY> </HTML> The second button (Xibology) should not fire an alert as the event is captured and cancelled.
presumably the form controls should be checking whether or not the event was canceled before passing along the events?
Reassigning to joki.
Assignee: jst → joki
Keywords: dom2
Component: DOM Level 2 → DOM Events
This isn't really a bug actually. preventDefault is used to prevent any default action by the browser but it doesn't stop anyone else from seeing the event. The browser doesn't actually do anything as a result of a button click, the action is being taken by another random JS listener. If these were submit buttons, for example, the default action would be to submit the form. That would be cancelled. If they were links, the link activation would be cancelled. If you want to stop the event from continuing to activate other event listeners on the page then use stopPropagation(). But preventDefault() won't do what you expect it to do here since the button action isn't being taken by the browser.
Status: NEW → RESOLVED
Closed: 25 years ago
Resolution: --- → INVALID
VERIFIED invalid
Status: RESOLVED → VERIFIED
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: