3
votes

The question is: should the disabled element produce an event that will be triggered on its parent(s)?

<div id="test">
  <button disabled="disabled">Click me</button>
</div>
<script type="text/javascript">

document.getElementById("test").onclick = function() {
  alert("Clicked!");
};

</script>

All browsers except IE prevent the event from being fired, but IE doesn't. Is this behavior documented or standardized? Which of browsers process the code above correctly?

1
Do you test this in IE in quirks mode or standards mode? - Marcel Korpel
The doctype is <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> - Sergei Morozov
I was wondering if you've found a solution/explanation. - Moss
No, I haven't. Just solved in some other way. - Sergei Morozov

1 Answers

0
votes

As per http://www.quirksmode.org/js/events_advanced.html I highly recommend to use event delegation instead of .onclick() binding. Example:

var element = document.getElementById('test'),
    doSomething = function () {
        alert("Clicked!");
    };
if (element.addEventListener) {
    element.addEventListener('click', doSomething, false);
} else if (element.attachEvent) {
    elem.attachEvent('onclick', doSomething);
}

:)