Event.stop for onclick?

2 views
Skip to first unread message

Stonk

unread,
Jul 10, 2009, 6:18:06 AM7/10/09
to Prototype & script.aculo.us
Hello,

I have two buttons <button onclick="foo()" class="save"
id="random_number"> and want to add an observer and fire an own
function (bar()). When this function is completed, foo() shall get
called.
I tried it with the event.stop, but that just doesn't work.

var requestDone = false;
function bar() {
// ....
requestDone = true;
}
// Wait till page is fully loaded
Event.observe(window, 'load', function() {
saveButtons = $$('button.save');
// Observe the Save buttons and fire the getCategories() function on
click
saveButtons.invoke('observe', 'click', function(event) {
if (false == requestDone) {
Event.stop(event);
}
});
saveButtons.invoke('observe', 'click', bar());
});

Could anyone please point me in the right direction?

T.J. Crowder

unread,
Jul 10, 2009, 9:48:36 AM7/10/09
to Prototype & script.aculo.us
Hi,

I'm guessing you're not in control of the HTML generation (e.g.,
you're getting it from some other thing -- a content management
solution, another site, whatever -- and don't have direct control over
it). If you *are* in control of the HTML generation, just don't
output the onclick attribute in the first place.

This came up a while back in the forums: Wanting to supersede a DOM0
event handler (e.g., a handler assigned via onclick rather than more
modern ways like those used by Element#observe). Here's a function
that looks for a button called btnGo and replaces its DOM0 handler
with a new handler, and even chains to the DOM0 handler afterward:

http://pastie.org/541315

The HTML page this would go on would have this button somewhere:

<input type='button' id='btnGo' value='Go' onclick='alert("\"Go\" got
clicked!");'/>

Obviously that's not generalized, but it should get you pointed the
right way...
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available
Reply all
Reply to author
Forward
0 new messages