HEEN
HEEN

Reputation: 4727

Javascript not working as Required

I have a javascript code like below which asks for checking atleast one checkbox and also for confirmation that whether I want to delete the data or NOT. But even after selectiong NO, it deletes the data:

Please see the code:-

function ValidateAll() {
        var chkselectcount = 0;
        var gridview = document.getElementById('<%= grdTeacherSalary.ClientID %>');
        for (var i = 0; i < gridview.getElementsByTagName("input").length; i++) {
            var node = gridview.getElementsByTagName("input")[i];

            if (node != null && node.type == "checkbox" && node.checked) {
                chkselectcount = chkselectcount + 1;
            }
        }
        if (chkselectcount == 0) {
            alert("Please select atleast One CheckBox");
            return false;
        }
        else {
            ConfirmationBox();
        }
    }
    function ConfirmationBox() {
        var result = confirm("Are you sure, you want to delete the Users ?");
        if (result) {
            return true;
        }
        else {
            return false;
        }
    }

UPDATE HTML:-

<asp:Button ID="btnDelete" runat="server" CausesValidation="false" CssClass="btn btn-danger" Text="Delete" OnClick="btnDelete_Click" OnClientClick="javascript:return ValidateAll();" />

Any help..!

Upvotes: 0

Views: 37

Answers (1)

Mox Shah
Mox Shah

Reputation: 3015

just update your code as

function ValidateAll() {
        var chkselectcount = 0;
        var gridview = document.getElementById('<%= grdTeacherSalary.ClientID %>');
        for (var i = 0; i < gridview.getElementsByTagName("input").length; i++) {
            var node = gridview.getElementsByTagName("input")[i];

            if (node != null && node.type == "checkbox" && node.checked) {
                chkselectcount = chkselectcount + 1;
            }
        }
        if (chkselectcount == 0) {
            alert("Please select atleast One CheckBox");
            return false;
        }
        else {
           //Instead of ConfirmationBox();
          return confirm("Are you sure, you want to delete the Users ?");
          // Or
          return ConfirmationBox();
        }
    }

Upvotes: 2

Related Questions