Jump to content
Larry Ullman's Book Forums

Recommended Posts

In the next to last bullet exercise on Chapter 8's Pursue, I tried substituting


U.addEvent(U.$('theForm'), 'submit', setHandlers);   // W3C form




U.$('theForm').onsubmit = setHandlers;  // traditional form


in events.js and it didn't work.  Why not?


What it seemed to do was the default behavior - reset the form, without calling any handlers.


On the other hand, I was able to add a second event listener to the form submission and it worked, i.e., this worked:


    U.$('theForm').onsubmit = setHandlers;                   // traditional
    U.addEvent(U.$('theForm'), 'submit', reportEvent);  // W3C


but this didn't:


    U.addEvent(U.$('theForm'), 'submit', setHandlers);  // W3C

    U.addEvent(U.$('theForm'), 'submit', reportEvent);  // W3C


They would seem to be equivalent, no?


Link to post
Share on other sites

Thanks.  No, no errors.   I was running in Eclipse, but when I pulled it up in the browser (Firefox), I saw that it actually was calling setHandlers but then form submission still seemed to occur.  Here's the current code:


// Script 8.8 - events.js

// Function called when events occur.
// Function reports the event type and target.
window.onload = function(){
    'use strict';
//    U.$('theForm').onsubmit = setHandlers;
    U.addEvent(U.$('theForm'), 'submit', setHandlers);
//    U.addEvent(U.$('theForm'), 'submit', reportEvent);

function setHandlers(e){
    'use strict';
    var events = ['mouseover','mouseout','click','keypress','blur'];
    for (var i=0, count=events.length; i < count; i++){
        var checkbox = U.$(events);
        if (checkbox.checked){
        else {
    if (typeof e == 'undefined') e=window.event;
    var target = e.target || e.srcElement;
    if (e.type == 'submit'){
        U.$('output').value = "HERE I AM\n";
    if (e.preventDefault){
    else {
        e.returnValue = false;
    return false;

function reportEvent(e){
    'use strict';
    if (typeof e == 'undefined') e = window.event;
    var target = e.target || e.srcElement;
    var msg = target.nodeName + ': ' + e.type + '\n';
    U.$('output').value += msg;

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...