admin管理员组文章数量:1291043
i'd like to know if its possible to call several function with one html button?
<button onclick:"functions()">Calls</button>
So could anyone try to explain to me if it is possible and how and maybe show me through some code snippets or anything.
i'd like to know if its possible to call several function with one html button?
<button onclick:"functions()">Calls</button>
So could anyone try to explain to me if it is possible and how and maybe show me through some code snippets or anything.
Share Improve this question edited May 9, 2020 at 4:24 ROOT 11.6k5 gold badges34 silver badges48 bronze badges asked May 9, 2020 at 4:16 Jean MiraJean Mira 491 silver badge6 bronze badges 2- 1 You can all one function from the button and inside the called function call other functions. – Imanpal Singh Commented May 9, 2020 at 4:18
- Does this answer your question? Call two functions from same onclick — first result when searching “js call multiple functions on button click”. Is there a specific problem you’re having with some attempt that you’re not showing us? – Sebastian Simon Commented May 9, 2020 at 4:47
4 Answers
Reset to default 6Yes it is possible, like so: This is the best option because you attach the event handler to the DOM node with javascript see this
html:
<button id="calls">Calls</button>
javascript:
document.getElementsById('calls').addEventListener('click', function(){
function1();
function2()
})
function function1() {
//whatever you want which will be called first by the button
}
function function2() {
//whatever you want which will be called second by the button
}
document.getElementById('calls').addEventListener('click', function(){
function1();
function2()
})
function function1() {
console.log("function1 was called first")
}
function function2() {
console.log("function2 was called second")
}
<button id="calls">Calls</button>
or so:
html:
<button onclick="function1();function2();">Calls</button>
javascript:
function function1() {
//whatever you want which will be called first by the button
}
function function2() {
//whatever you want which will be called second by the button
}
function function1() {
console.log("function1 was called first")
}
function function2() {
console.log("function2 was called second")
}
<button onclick="function1();function2();">Calls</button>
or so:
html:
<button onclick="functions()">Calls</button>
javascript:
function functions() {
function1();
function2();
}
function function1() {
//whatever you want which will be called first by the button
}
function function2() {
//whatever you want which will be called second by the button
}
function functions() {
function1();
function2();
}
function function1() {
console.log("function1 was called first")
}
function function2() {
console.log("function2 was called second")
}
<button onclick="functions()">Calls</button>
First of all, you're better off not using onclick
at all and attaching the event handler to the DOM node through your Javascript code. This is known as Unobtrusive_JavaScript.
In order to bind multiple functions to one button, you can either do this (But it is not preferred at all):
function fn(){
console.log('fn is called!');
}
function fn2(){
console.log('fn2 is called!');
}
function fn3(){
console.log('fn3 is called!');
}
<button onclick="fn(); fn2(); fn3();">Calls</button>
Or bind your function with addEventListener
like this (This is the most preferred approach for calling multiple functions):
document.getElementsByTagName('button')[0].addEventListener('click', function(){
fn();
fn2();
fn3();
})
function fn() {
console.log('fn is called!');
}
function fn2() {
console.log('fn2 is called!');
}
function fn3() {
console.log('fn3 is called!');
}
<button>Calls</button>
You can just do that:
<button onclick="function1();function2()">Calls</button>
you can execute multiple function by order
function f1()
{
alert("execution of f1");
}
function f2()
{
alert("execution of f2");
}
function f3()
{
alert("execution of f3");
}
<button onclick="f1();f2();f3();">Click Me</button>
本文标签: call multiple function with one button javascriptStack Overflow
版权声明:本文标题:call multiple function with one button javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741521051a2383185.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论