[thelist] Calling multiple functions in single onclick

Christian Heilmann codepo8 at gmail.com
Mon Aug 1 10:10:02 CDT 2005


> Hello All,
> 
> Newbie here. I have a form which is submitted by a
> click on a link:
> 
> <a href="#" onclick="return
> document.forms.orderform.submit();">checkout</a>
> 
> If I wanted to call another javascript function
> (logevent()) upon the onclick event prior to
> document.forms.orderform.submit(), what would the code
> look like?

What about users without JavaScript? They will get a link that does
nothing whatsoever. At least provide them with a fallback.

Give your form a normal submit button and do an onsubmit on the form:

<form action="checkout.php" onsubmit="logevent();">
<input type="submit" value="checkout" />

If you need more stuff to happen write a function, place it in a
script tag in the head and call this one instead.

<form action="checkout.php" onsubmit="myfunc();">
<input type="submit" value="checkout" />

<script type="text/javascript">
function myfunc()
{
logevent();
callmom();
washsocks();
return true;
}
</script>

More:
http://www.onlinetools.org/articles/unobtrusivejavascript/chapter5.html

Over 3 years old and still quite necessary it seems:
http://www.evolt.org/article/Forms_and_JavaScript_Living_Together_in_Harmony/17/28553/

-- 
Chris Heilmann 
Blog: http://www.wait-till-i.com
Writing: http://icant.co.uk/  
Binaries: http://www.onlinetools.org/


More information about the thelist mailing list