user583181
user583181

Reputation: 75

What's the problem with this code?

Where to modify this code

IT STILL GIVES A MSGBOX IF I SELECT THE CHECKBOX BOX OR NOT ....

My code below will redirect to Google in both conditions: If the user selects the checkbox, then it will redirect to www.google.com, but if a user forgets to check the checkbox then it shows the msg box with an ok button. When I click on ok it should redirect to www.google.com

I want

When a user forgets to check any of the checkboxes to show a msgbox with an ok button and stay on the same page. Otherwise if user selects any of the checkboxes then redirect to www.google.com

What's wrong with this code?

    <title>Untitled Page</title>
    </head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:CheckBox ID="CheckBox1" runat="server" />
        <asp:CheckBox ID="CheckBox2" runat="server" />

    </div>
    <asp:Button ID="Button1" runat="server" OnClientClick ="ConfirmSelection(this.form)" Text="Button" />


    </form>
    <script type="text/javascript">
function ConfirmSelection(frm) 
{ 
   for (i=0; i<=1; i++) {
     //chkSubjectOfInterest is the id of your checkbox control

     if (frm.elements[i].name.indexOf('chkSubjectOfInterest') !=-1) 
     { 
       if (frm.elements[i].checked) 
       { 
         return true
       } 
     } 
   } 
   alert('You havent selected an Item yet!')
   return false
}
</script>
</body>
</html>

Upvotes: 0

Views: 135

Answers (1)

Julius A
Julius A

Reputation: 39632

I think you need a return in your function call. Also ensure your function ConfirmSelection has explicit return value in both parts of the if statements

OnClientClick ="return ConfirmSelection(this.form);"

Upvotes: 1

Related Questions