Click here to Skip to main content
15,922,533 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Is it possible to call two javascript function in a button's OnClick event.
If possible how can I implement this.Please help me
Posted

See below..

1. Through on client click event
XML
<script type="text/javascript">
    function myfunc1()
    {
        alert('function 1');
    }
    function myfunc2()
    {
        alert('function 2');
    }
 </script>

Html below..
ASP.NET
<asp:button id="Button1" runat="server" text="Button" onclientclick="myfunc1(), myfunc2()" xmlns:asp="#unknown" /> // calling two functions

2. Through JQuery button click event
XML
<script type="text/javascript">
    function myfunc1()
    {
        alert('function 1');
    }
    function myfunc2()
    {
        alert('function 2');
    }
    $(document).ready(function()
    {
        $("#Button2").click(function()
        {
            myfunc1();
            myfunc2();
        });

    });
 </script>

Html below..
ASP.NET
<asp:button id="Button2" runat="server" text="Button" xmlns:asp="#unknown" />

Hope it helps you..
 
Share this answer
 
v2
Comments
Joezer BH 21-Mar-13 5:59am    
5+
vinodkumarnie 21-Mar-13 6:05am    
Thank you Edo Tzumer..
Why not? Call the second JavaScript Function in 1st JavaScript Function.
Try this:
HTML:
ASP.NET
<asp:button id="btn" runat="server" onclientclick="javasctipt:return myFunction1();">
</asp:button>

JavaScript:
JavaScript
function myFunction1()
{
    alert("Funcation 1 executed");
    myFunction2();
}
function myFunction2()
{
    alert("Funcation 2 executed");
}



--Amit
 
Share this answer
 
v2
Comments
[Edit]
Removed xmlns:asp="#unknown" and extra button tags.
[/Edit]
Joezer BH 21-Mar-13 5:58am    
It's obvious ;)
but still 5+
fjdiewornncalwe 21-Mar-13 17:59pm    
What if myFunction2 does not always run after myFunction1? In that case this introduces a bug.
_Amy 22-Mar-13 0:08am    
I think you din't saw the question. The Question is "Is it possible to call two javascript function in a button's OnClick event?". And you know very well that can be done in the function by calling another function. I just gave an example. OP need to handle the error there. He should not FULLY DEPEND on the answer given by me or anyone in CP. Anyway thanks for pointing out the bug.

--Amit

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900