Is it okay to use it twice like this:
onClick=function1() onClick=function2()
?
Is it okay to use it twice like this:
onClick=function1() onClick=function2()
?
You can do this with addEventListener
.
var button = document.getElementById('yourButton');
button.addEventListener('click', function(event) {
// call function 1 here
// call function 2 here
console.log('test');
});
<button id="yourButton">Click me!</button>
No. If you want to call both functions in one on click you would need to do something like
function clicked(){
function1();
function2();
}
function function1(){
console.log('function1');
}
function function2(){
console.log('function2');
}
<button id="yourButton" onClick='clicked()'>Click me!</button>
Like the one commented to you, the second will override the first.
What you need to do is to call foo() and inside foo call the two functions you need:
function foo() {
func1();
func2();
}
As suggested by others use addEventListener. It gives you the flexibility later on to remove the added listener by calling removeEventListener