Forum Moderators: open

Message Too Old, No Replies

Why doesn't this stop the form submitting?

         

Dabrowski

10:02 pm on Sep 29, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why doesn't this work?

(obviously I have looked up the form element first)

addEvent( form, "submit", function() { alert( "hello"); return false; });

It does the alert, and then it submits anyway.

Fotiman

3:39 am on Sep 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It doesn't work because this is an event "listener", not an event handler. This isn't the same as doing <form onsubmit="...; return false;"> because you can attach multiple event listeners, while you can have only 1 event handler. So what you need to do instead of return false is stop the event. Here's how you'd do it with the Yahoo UI Library:

YAHOO.util.Event.on( form, 'submit', function(e) { 
YAHOO.util.Event.stopEvent(e);
alert("hello");
});

Dabrowski

10:27 pm on Sep 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



How would I do that if my computer can't recieve YUI?

;)

btw, left a comment on ur website.

Fotiman

1:31 pm on Oct 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Why would your computer not be able to receive YUI?

btw, I replied to your comment. :-)

Dabrowski

5:50 pm on Oct 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know, lots of reasons. Network outage, temporary DNS problems, AVG goes on a mad one.

Anything could happen, and when it does I don't want my websites to stop working.

Anyway, that's besides the point, yes I know that YUI is very good and very function and can do anything I want to do. As can script.aculo.us, jQuery, the various dollar functions...

...but what they don't do is TEACH. I want to know how it's done, so I can improve my own code, and expand my own knowledge. I may be able to take techniques from this to other languages I code in, and other projects.

Fotiman

6:05 pm on Oct 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can place a copy of YUI on your site along side your other html and JavaScript files... you don't need to point to their hosted version.

As for stopping the event, it's again browser specific.

Assuming you have a reference to the event (ev), the following will stop the event from propagating up the DOM and prevent the default browser action:


if (ev.stopPropagation) {
ev.stopPropagation();
}
else {
ev.cancelBubble = true;
}
if (ev.preventDefault) {
ev.preventDefault();
}
else {
ev.returnValue = false;
}

Dabrowski

9:09 pm on Oct 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I really need to read up on event handling. I'll admit, while I know a lot about most JS, events really aren't my strong point. As long as I've had addEvent, that's pretty much where my knowledge stopped.

AJAX is my worst, but don't tell anyone

Thanks for that little snippet, I'll see if I can get it to work ok now.

P.S. nice reply. Maybe you just got caught up in the moment with that one.