Skip to content

Instantly share code, notes, and snippets.

@connor
Created March 29, 2012 00:54

Revisions

  1. Connor Montgomery revised this gist Mar 29, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions introduction to events.markdown
    Original file line number Diff line number Diff line change
    @@ -34,11 +34,11 @@ An example of using an event listener is like so:
    ```


    <javascript>
    ```js
    var element = document.getElementById('myButton');
    element.addEventListener('click', function() {
    alert("This is good!");
    }, false)
    </javascript>
    ```

    Like mentioned in the previous section, using event listeners have several advantages, such as: separating the markup (HTML) from the behavior (JavaScript) and not duplicating code. This is what people mean by using *Unobtrusive JavaScript*: it stays separate from your markup and doesn't repeat itself.
  2. Connor Montgomery revised this gist Mar 29, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions introduction to events.markdown
    Original file line number Diff line number Diff line change
    @@ -34,11 +34,11 @@ An example of using an event listener is like so:
    ```


    ```js
    <javascript>
    var element = document.getElementById('myButton');
    element.addEventListener('click', function() {
    alert("This is good!");
    }, false)
    ```
    </javascript>

    Like mentioned in the previous section, using event listeners have several advantages, such as: separating the markup (HTML) from the behavior (JavaScript) and not duplicating code. This is what people mean by using *Unobtrusive JavaScript*: it stays separate from your markup and doesn't repeat itself.
  3. Connor Montgomery revised this gist Mar 29, 2012. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions introduction to events.markdown
    Original file line number Diff line number Diff line change
    @@ -29,16 +29,16 @@ Instead, it's best to separate your markup from your logic by using event listen

    An example of using an event listener is like so:

    <markup>
    ```html
    <button id="myButton">Click Me!</button>
    </markup>
    ```


    <javascript>
    ```js
    var element = document.getElementById('myButton');
    element.addEventListener('click', function() {
    alert("This is good!");
    }, false)
    </javascript>
    ```

    Like mentioned in the previous section, using event listeners have several advantages, such as: separating the markup (HTML) from the behavior (JavaScript) and not duplicating code. This is what people mean by using *Unobtrusive JavaScript*: it stays separate from your markup and doesn't repeat itself.
  4. Connor Montgomery revised this gist Mar 29, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions introduction to events.markdown
    Original file line number Diff line number Diff line change
    @@ -17,9 +17,9 @@ There are two general ways that we can perform an action when an event occurs:

    An example of using an inline event handler is like so:

    <markup>
    ```html
    <button id="myButton" onclick="alert('This is bad');">Click Me!</button>
    </markup>
    ```

    As you probably guessed from reading what was within the `alert` - using inline event handlers is a bad practice that we **don't recommend**.

  5. Connor Montgomery created this gist Mar 29, 2012.
    44 changes: 44 additions & 0 deletions introduction to events.markdown
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,44 @@
    ## Introduction to Events

    Events are at the heart of most pages utilizing JavaScript. For our JavaScript to perform an action, we must wait for a specific event to happen, and then execute the predefined action.

    ### What an event is

    While it was hinted at earlier, events are **things that happen on the page**. The majority of events utilized in JavaScript applications are user-driven, like: clicking a button, hovering over an element, and pressing a key. However, there are many other events that the browser fires, like: loading all of the resources on a page or going offline. MDN has a good reference of [available DOM events](https://developer.mozilla.org/en/DOM/DOM_event_reference).

    ### Performing an action when an event happens

    Now that we have a basic understanding of what DOM events are, we can perform an action when an event occurs.

    There are two general ways that we can perform an action when an event occurs: `inline event handlers` and `event listeners`.


    ##### Inline Event Handlers

    An example of using an inline event handler is like so:

    <markup>
    <button id="myButton" onclick="alert('This is bad');">Click Me!</button>
    </markup>

    As you probably guessed from reading what was within the `alert` - using inline event handlers is a bad practice that we **don't recommend**.

    Instead, it's best to separate your markup from your logic by using event listeners.

    ##### Event Listeners

    An example of using an event listener is like so:

    <markup>
    <button id="myButton">Click Me!</button>
    </markup>


    <javascript>
    var element = document.getElementById('myButton');
    element.addEventListener('click', function() {
    alert("This is good!");
    }, false)
    </javascript>

    Like mentioned in the previous section, using event listeners have several advantages, such as: separating the markup (HTML) from the behavior (JavaScript) and not duplicating code. This is what people mean by using *Unobtrusive JavaScript*: it stays separate from your markup and doesn't repeat itself.