JavaScript from Null: Chapter 5
basixvideos

JavaScript from Null: Chapter 5 – Events

This entry is part 5 of 7 in the series javascript-from-null

The Full Series

As we move forward with JavaScript University, today, we’ll learn how to add event handlers to elements on the page. Unfortunately, this can be more cumbersome than we’d hope, due to the fact that Internet Explorer must always be the black sheep. Nevertheless, we’ll learn how to abstract these inconsistencies away to our custom utility function.

As with every JavaScript from Null screencast, it’s not essential that you view the previous entries in the series before watching.

In this Screencast, you’ll Learn About:

  • Working with .addEventListener
  • Working with IE’s .attachEvent
  • Differences in the way browsers handle events.
  • Capturing phase vs. event bubbling
  • Building a utility function to abstract our cross-browser event handling.

Chapter 5: Events

Other Viewing Options

Ready to take your skills to the next level, and start profiting from your scripts and components? Check out our sister marketplace, CodeCanyon.

CodeCanyon


Series Navigation«JavaScript from Null: Chapter 4JavaScript from Null: Cross-Browser Event Binding»

Add Comment

Discussion 67 Comments

Comment Page 2 of 2 1 2
  1. Martin says:

    Why was my comment deleted?

  2. cid says:

    Hi Jeff,

    It’s a fine intro but you say nothing about why this is better than the inline model which is what everyone had used in the past and many still use. Other than separating behavior and structure for its own sake why should we use this obviously more complicated model instead of just inserting HTML event attributes?
    Thanks

  3. cid says:

    Or for that matter why shouldn’t we use this instead, which works fine on everything from IE5 to Chrome

    window.onload=function change_color()
    {
    var anchor=document.getElementById(‘anchor’)

    anchor.onmouseover=function (){ document.body.bgColor=”red”}
    anchor.onmouseout=function (){ document.body.bgColor=”blue”}
    }

    Move over me

    • Jeffrey Way says:
      Author

      For some cases, it’s works just fine. But you can’t assign multiple event handlers with that method.

      • cid says:

        You mean to the same element? Looks to me I can assign as many as I want

        window.onload=function addEvents()
        {
        var anchor=document.getElementById(‘anchor’)

        anchor.onmouseover=function (){ document.body.bgColor=”red”}
        anchor.onmouseout=function (){ document.body.bgColor=”blue”}
        anchor.onclick=function (){ document.body.bgColor=”yellow”}
        anchor.ondblclick=function (){ document.body.bgColor=”gray”}

        }

        Is this somehow wrong?

      • Jeffrey Way says:
        Author

        @cid – Try to add another function to that event handler. Only the latter will execute.

      • cid says:

        Indeed this doesn’t work:

        anchor.onclick =function (){ document.body.bgColor=”red”}
        anchor.onclick =function (){ div.style.backgroundColor=”gray” }

        but this does

        window.onload=function addEvents()
        {

        var anchor=document.getElementById(‘anchor’)
        var div=document.getElementById(‘div’)

        anchor.onclick=function(){first();second();}
        anchor.ondblclick=function(){first2();second2();}

        function first(){ document.body.bgColor=”red”}
        function second(){div.style.backgroundColor=”yellow” }
        function first2(){ document.body.bgColor=”blue”}
        function second2(){div.style.backgroundColor=”gray” }
        }

  4. Jimmy says:

    I love this, but it is sooo damn fast, that I can barely understand what I’m doing. I think this is a solid introduction, but will need a legit guide so that I’m not flying quite so blind.

  5. Ridwan says:

    thanks for the tuts, really great.

    can someone explain why window.attachEvent replaced by obj.attachEvent.

    my question is obj value is var anchor = document.getElementById(‘anchor’),

    why anchor can replace window is it same between window and var anchor =

    document.getElementById(‘anchor’) still confused. thanks in advance

Comment Page 2 of 2 1 2

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.