TAGS :Viewed: 9 - Published at: a few seconds ago

[ Multiple button calling same function and execute different methods ]

I have two buttons calling the same function: btnUpdate has to execute this function, whereas btnSave has no need to execute this method:

function Update_New() {
  if (document.getElementById("btnUpdate").value == 2) {
    var update_confrimation = confirm("Are you sure you want to Update?");
    if (update_confrimation == true) {
      return true;
    } else {
      return false;
    }
  }
}

Code for the buttons:

<asp:Button ID="btnSave" runat="server" Text="SAVE" OnClick="btnSave_Click" OnClientClick="return Update_New(event)" value="1" />
<asp:Button ID="btnUpdate" runat="server" Text="UPDATE" OnClick="btnUpdate_Click" OnClientClick="return Update_New(event) " value="2" />

My Problem: When I click the update button, the function is not working.

Answer 1


You need to pass the reference to the clicked button to the method

<asp:Button ID="btnSave" runat="server" Text="SAVE" OnClick="btnSave_Click" OnClientClick="return Update_New(this)" value="1" />
<asp:Button ID="btnUpdate" runat="server" Text="UPDATE" OnClick="btnUpdate_Click" OnClientClick="return Update_New(this) " value="2" />

then using the clicked button's value you can show different messages if you want

function Update_New(el) {
    return confirm(el.value == 2 ? "Are you sure you want to Update?" : 'Are you sure you want to save');
}

Answer 2


<asp:Button ID="btnSave" runat="server" Text="SAVE" OnClick="btnSave_Click" OnClientClick="return Update_New(this)" value="1" />
<asp:Button ID="btnUpdate" runat="server" Text="UPDATE" OnClick="btnUpdate_Click" OnClientClick="return Update_New(this) " value="2" />

just use "this" key instead of "event" and then in javascript use variable to store object( i have used variable X ).

     function Update_New(this) {
              if (this.value == 2) 
                    {
                    var update_confrimation = confirm("Are you sure you want to Update?");
                    if (update_confrimation == true) {
                        return true;
                    }
                    else {

                        return false;
                          }
                  }

}

Answer 3


You have a problem with your first if statement. Your button's text after render will be value so your if statement should be like this:

if (document.getElementById("btnUpdate").value === "UPDATE")

Also you have missed one } at the end of your code and also it is better using === instead of ==. So your code should be like this:

function Update_New() {
        if (document.getElementById("btnUpdate").value === "UPDATE") {
            var update_confrimation = confirm("Are you sure you want to Update?");
            if (update_confrimation === true) {
                return true;
            } else {

            return false;
        }
    }
}

Please Note: Not all code paths return a value so consider adding one more return statement at the end.