How to call 2 functions in one onclick?

what’s the correct syntax to call 2 functions from an onclick attribute? the code below doesnt work, maybe im putting semi colons in the wrong place? since the second function needs the return false, im guessing i should put that one last since return false would stop anything after that from running.

<a onclick='return confirm("are you sure?"); some_function(param1, param2 ); return false;'>test</a>

am i on the right track?


Your code

<a onclick=[B][I]'return confirm("are you sure?");[/I][/B]
some_function(param1, param2 );
return false;'>test</a>

has the problem that you are returning a value from the first item you call, hence some_function() don’t get a look in.

ok makes sense. but it must be possible to call 2 functions from an onclick, no? afaik the “return” in “return confirm” is required and cant be removed

You can call two functions, but the first one’s value can’t be returned to the environment. I would do this:

<a onclick='return check()'>test</a>
<script>           Somewhere else in the file, or externally
function check()
  if( confirm( "are you sure" ) )
    some_function( param1, param2 )

  return false;

I’m assuming that you only want to do some_function() based on the user confirming.

JavaScript doesn’t allow you to register more than one ‘onclick’ type event handler for an event. You are left with two choices: first, lump together the two function in a single wrapper function and register it as the onclick event handler; second, use the advanced attachEvent/addEventListener model to register multiple handlers.

Example of the first technique.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<html xmlns="">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
    window.onload = function() {
        var trigger = document.getElementById('trigger');
        trigger.onclick = function(){
            var returnVal = confirm('Are You Sure?');
            return returnVal;
    function second_function(){
        var trigger= document.getElementById('trigger');
        trigger.firstChild.nodeValue = 'Clicked';
    <a id="trigger" href="#">Click Me</a>

Maybe you should try it otherwise

i managed to get it to work: if(first function){run second function}