You can call multiple functions from the same onClick
event in JavaScript by chaining the function calls together using the ;
operator, like this:
<button onclick="function1(); function2();">Click me</button>
Alternatively, you can define a separate function that calls the other two functions and assign it to the onClick
event:
<button onclick="combinedFunction()">Click me</button>
<script>
function function1() {
// function 1 code
}
function function2() {
// function 2 code
}
function combinedFunction() {
function1();
function2();
}
</script>
You can also use javascript addEventListener
method
<button id="myBtn">Click me</button>
<script>
function function1() {
// function 1 code
}
function function2() {
// function 2 code
}
var btn = document.getElementById("myBtn");
btn.addEventListener("click", function1);
btn.addEventListener("click", function2);
</script>
In both cases, both function1 and function2 will be called when the button is clicked.
Also Read:
- Difference Between encodeURIComponent() And encodeURI()
- Convert Comma Separated String Into An Array In JavaScript
- How To Check If A String Is Empty In JavaScript
- How To Sort An Array Of Numbers In JavaScript
- How To Return Multiple Values From A Function In JavaScript
- How To Get The Current URL With JavaScript
- How To Include A JavaScript File In Another JavaScript File
- How To Detect Screen Resolution With JavaScript
- How To parse JSON Into JavaScript
- How To Add Elements To An Array In JavaScript
- How To Generate A Timestamp In JavaScript
- How To Convert A JavaScript Object To JSON String
- How To Add An Element To The Beginning Of An Array
- How To Get The Value From The Input Field In JavaScript
- Adjust The iFrame Height To Fit With Content In JavaScript
- Call Two Functions From The Same onClick Event In JavaScript
- How To Detect When A Window Is Resized Using JavaScript
- How To Reset A Form With JavaScript
- How To Pass JavaScript Variables To PHP
- Difference Between PHP And JavaScript
- Armstrong Number In JavaScript
- How To Determine If A Number Is Odd Or Even In JavaScript
- How To Check If A Number Is A Palindrome In JavaScript
- How To Convert Strings To Uppercase In JavaScript
- How To Convert A String To Lowercase In JavaScript
- Code To Check If Age Is Not Less Than 18 Years In JavaScript
- How To Reverse A Number In JavaScript
- How To Check If A Number Is Prime Using JavaScript
- How To Find Factorial Of A Number In JavaScript
- Sum Of Two Numbers In JavaScript
- How To Display The Current Date And Time In JavaScript