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>
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
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" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<script type="text/javascript">
window.onload = function() {
var trigger = document.getElementById('trigger');
trigger.onclick = function(){
var returnVal = confirm('Are You Sure?');
second_function();
return returnVal;
}
}
function second_function(){
var trigger= document.getElementById('trigger');
trigger.firstChild.nodeValue = 'Clicked';
}
</script>
<body>
<a id="trigger" href="#">Click Me</a>
</body>
</html>