Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling multiple JavaScript functions on a button click

How to call multiple functions on button click event?

Here is my button,

<asp:LinkButton
    ID="LbSearch"
    runat="server"
    CssClass="regular"
    onclick="LbSearch_Click"
    OnClientClick="return validateView();ShowDiv1();">

But my ShowDiv1 doesn't get called...

My JavaScript functions:

function ShowDiv1() {
    document.getElementById("ReportDiv").style.display = 'block';
    return false;
}

function validateView() {

    if (document.getElementById("ctl00_ContentPlaceHolder1_DLCategory").selectedIndex == 0) {
        document.getElementById("ctl00_ContentPlaceHolder1_ErrorMsg").innerHTML = "Please Select Your Category";
        document.getElementById("ctl00_ContentPlaceHolder1_DLCategory").focus();
        return false;
    }
    if (document.getElementById("ctl00_ContentPlaceHolder1_DLEmpName").selectedIndex == 0) {
        document.getElementById("ctl00_ContentPlaceHolder1_ErrorMsg").innerHTML = "Please Select Your Employee Name";
        document.getElementById("ctl00_ContentPlaceHolder1_DLEmpName").focus();
        return false;
    }
    return true;
}

If the ValidateView() function returns true I should call ShowDiv1().

like image 696
ACP Avatar asked Mar 10 '10 04:03

ACP


People also ask

Can we call 2 JavaScript functions onClick?

Greetings! Yes, you can call two JS Function on one onClick.

Can I call multiple functions onClick?

Given multiple functions, the task is to call them by just one onclick event using JavaScript. Here are few methods discussed. Either we can call them by mentioning their names with element where onclick event occurs or first call a single function and all the other functions are called inside that function.

How do I call multiple functions in JavaScript?

Answer: Use the addEventListener() Method.

Can you have multiple functions in JavaScript?

Function chaining is a pattern in JavaScript where multiple functions are called on the same object consecutively. Using the same object reference, multiple functions can be invoked. It increases the readability of the code and means less redundancy.


2 Answers

Because you're returning from the first method call, the second doesn't execute.

Try something like

OnClientClick="var b = validateView();ShowDiv1(); return b"

or reverse the situation,

OnClientClick="ShowDiv1();return validateView();"

or if there is a dependency of div1 on the validation routine.

OnClientClick="var b = validateView(); if (b) ShowDiv1(); return b"

What might be best is to encapsulate multiple inline statements into a mini function like so, to simplify the call:

// change logic to suit taste
function clicked()  {
    var b = validateView(); 
    if (b) 
        ShowDiv1()
    return b;
}

and then

OnClientClick="return clicked();"
like image 163
John K Avatar answered Oct 18 '22 14:10

John K


That's because, it gets returned after validateView();;

Use this:

OnClientClick="var ret = validateView();ShowDiv1(); return ret;"
like image 31
raj Avatar answered Oct 18 '22 14:10

raj