49 - Handling events with JavaScript

Forums » Dev.Opera » Archived Article Discussions

This topic has been closed. No new entries allowed.

Reason: You can now post comments on articles on Dev Opera

Forum rules and guidelines

You need to be logged in to post in the forums. If you do not have an account, please sign up first.

Go to last post

3. February 2009, 06:38:18

robnyman

Posts: 0

49 - Handling events with JavaScript

You can't do anything with JavaScript in response to user actions without using events. In fact, they have featured in some of the previous articles without really being explained. In this article, Robert Nyman chronicles at the evolution of JavaScript/HTML events, before looking at how events are made to work across different browsers in modern JavaScripting. Concepts such as bubbling and event object references are covered.

( Read the article )

16. March 2009, 16:12:37

stelt

Posts: 59

typo: shouldnote

HTML of "Stopping event bubbling" is wrong:
<div>.....</a>

6. April 2009, 23:33:46

Opera Software

chrismills

Posts: 379

Originally posted by stelt:

typo: shouldnote

HTML of "Stopping event bubbling" is wrong:

.....



Typos fixed - thanks!

I've also improved the code indenting.
Chris Mills
Developer Relations Manager
Editor, dev.opera.com and labs.opera.com

22. January 2011, 00:31:04 (edited)

SamatJain

Posts: 1

Under "Applying events to certain elements", the example:
addEvent(window, "load", function () {
  var links = document.getElementsByTagName("a");
    for (var i=0; i<links.length; i++) {
      addEvent(links[i], "click", function () {
        alert("NOPE! I won't take you there!");

        // This line's support added through the addEvent function. See below.
      evt.preventDefault(); 
    });
  }
});

uses "evt" without having declared it. I don't know if this is an implicitly-passed variable for event handlers (it's not mentioned), but other examples in the article explicitly define it as an parameter to the event handler.

In "Stopping event bubbling", the HTML is wrong:
<div>
  <ul>
    <li>
      <a href="http://www.opera.com/" id="stop-default">Opera</a>
    </li>
    <li>
      <a href="http://www.opera.com/products/dragonfly/" id="stop-default">Opera Dragonfly</a>
    </li>
  </ul>	
</div>


Values for the "id" attribute are supposed to be unique for a document. They're not here — each <a> has the same id. I'm not sure what the code that uses this HTML does.

Forums » Dev.Opera » Archived Article Discussions