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

 

for

 

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?

 

Share this post


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){
            U.addEvent(document,events,reportEvent);
        }
        else {
            U.removeEvent(document,events,reportEvent);
        }
    }
    if (typeof e == 'undefined') e=window.event;
    var target = e.target || e.srcElement;
    
    if (e.type == 'submit'){
        U.$('output').value = "HERE I AM\n";
        reportEvent(e);
    }
    
    if (e.preventDefault){
        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;
}

Share this post


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.

Guest
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.

Loading...

×
×
  • Create New...