0

I wanted to run my onclick event after the onclientclick but the program keep pop up and ask Please select at least one to delete no matter my checkbox is clicked or not.

I think maybe my return value is wrong? Thanks for help.

 <script type="text/javascript">
        function validate() {
            var gridView = document.getElementById("<%=grid1.ClientID %>");
            var checkBoxes = gridView.getElementsByTagName("input");
            for (var i = 0; i < checkBoxes.length; i++) {
                if (checkBoxes[i].checked) {
                    if (confirm("Are you sure to delete?")) {
                        alert("Ok");
                    }
                }
                else{
                    alert("Please select at least one to delete.");
                    return false;
                }
                return false;
            }
       }

    </script>
2
  • before your for(...){} loop, if you put console.log(checkBoxes); what shows in the console? Commented Sep 12, 2015 at 7:41
  • your loop will just run once Commented Sep 12, 2015 at 7:43

1 Answer 1

1

Leave the function after the confirm:

 <script type="text/javascript">
    function validate() {
        var gridView = document.getElementById("<%=grid1.ClientID %>");
        var checkBoxes = gridView.querySelectorAll("input[type='checkbox']");
        for (var i = 0; i < checkBoxes.length; i++) {
            if (checkBoxes[i].checked) {
                if (confirm("Are you sure to delete?")) {
                    alert("Ok");
                    return true;
                }

            }
        }
        alert("Please select at least one to delete.");
        return false;
   }

</script>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.