Is there any way to use the onClick
html attribute to call more than one JavaScript method?
EDIT:
Thanks for all of the answers guys! :)
Is there any way to use the onClick
html attribute to call more than one JavaScript method?
EDIT:
Thanks for all of the answers guys! :)
Sure, simply bind multiple listeners to it.
Short cutting with jQuery
$("#id").bind("click", function () { alert("Event 1"); });
$(".foo").bind("click", function () { alert("Foo class"); });
<div class="foo" id="id">Click</div>
onClick="doSomething();doSomethingElse();"
But really you're better off not using onClick at all and attaching the event handler to the DOM node through your JS code. This is known as unobtrusive javascript.
A link with 1 function defined
<a href="#" onclick="someFunc()">Click me To fire some functions</a>
Firing multiple functions from someFunc()
function someFunc() {
showAlert();
validate();
anotherFunction();
YetAnotherFunction();
}
I would use the element.addEventListener method to link it to a function. From that function you can call multiple functions. This could be useful...
https://developer.mozilla.org/en/DOM/element.addEventListener
The advantage I see in binding an event to a single function and then calling multiple functions is that you can perform some error checking, have some if else statements so that some functions only get called if certain criteria is met.