how to display message if none of the checkbox is selected

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mukeshrasm
    Contributor
    • Nov 2007
    • 254

    how to display message if none of the checkbox is selected

    Hi

    I am deleting records from database using checkbox. means only that record will be deleted from database which are selected in checkbox. and before deleting record I am displaying a message "Are you sure to delete the selected records" using javascript.and when user clicks OK, record is deleted.

    Here in message I want it should like "Are you sure to delete 5 records" if user selects 5 records. So how can I do this using javascript.

    And if user without selecting any checkbox and clicks Delete button then it should display message "no record is selected. please select" using javascript so how can I do this in javascript.

    Name of the checkbox is chkdelete.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Originally posted by mukeshrasm
    Here in message I want it should like "Are you sure to delete 5 records" if user selects 5 records. So how can I do this using javascript.

    And if user without selecting any checkbox and clicks Delete button then it should display message "no record is selected. please select" using javascript so how can I do this in javascript.
    you can count the number of checked checkboxes. use getElementsByNa me() to get all boxes and test the checkbox.checked property for each one.

    Comment

    • mukeshrasm
      Contributor
      • Nov 2007
      • 254

      #3
      Originally posted by Dormilich
      you can count the number of checked checkboxes. use getElementsByNa me() to get all boxes and test the checkbox.checked property for each one.
      How can I count the number of checked checkboxes.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        Originally posted by mukeshrasm
        How can I count the number of checked checkboxes.
        getElementsByNa me("checkdelete ") will give you all the checkboxes (as array*). loop through this array (using for-loop) and test if the checked property is true (checked) or false (unchecked), if it is true, increment you counter variable (for checked checkboxes).

        * it is actually a HTMLElementColl ection, which is an Object (with numerical properties) rather than an Array. The difference is that this object doesn't contain most methods of the Array object (like pop(), push(), shift(), ...).

        Comment

        • mukeshrasm
          Contributor
          • Nov 2007
          • 254

          #5
          Originally posted by Dormilich
          getElementsByNa me("checkdelete ") will give you all the checkboxes (as array*). loop through this array (using for-loop) and test if the checked property is true (checked) or false (unchecked), if it is true, increment you counter variable (for checked checkboxes).

          * it is actually a HTMLElementColl ection, which is an Object (with numerical properties) rather than an Array. The difference is that this object doesn't contain most methods of the Array object (like pop(), push(), shift(), ...).

          I am doing in this way:
          Code:
          function check()
          {
          	var a=document.getElementsByName("checkbox"
          	var j=0
          		for(i=0;i<=a.length;i++)
          		{
          			if(a[i].checked==true)
          			{
          				 j=j+1;
          			}
          		}
                 if (j==0)
                 {
                    alert("please select checkbox")
                 }
          
          }
          but it is not working. not sure where I am wrong

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            Originally posted by mukeshrasm
            it is not working. not sure where I am wrong
            hard to tell without the HTML code.

            check if the variables you refer to actually exist, plus you didn't specify an action if at least one checkbox was selected.

            PS: you can replace j = j+1; by j++;

            Comment

            • mukeshrasm
              Contributor
              • Nov 2007
              • 254

              #7
              Originally posted by Dormilich
              hard to tell without the HTML code.

              check if the variables you refer to actually exist, plus you didn't specify an action if at least one checkbox was selected.

              PS: you can replace j = j+1; by j++;
              here is the html
              Code:
              <form id="form1" name="form1" method="post" action="" >
               <input type="checkbox" name="checkbox" value="checkbox" />
                </p>
                <p>
                  <input type="checkbox" name="checkbox" value="checkbox" />
                </p>
                <p>
                  <input type="checkbox" name="checkbox" value="checkbox" />
                </p>
                <p>
                  <input type="checkbox" name="checkbox" value="checkbox" />
               <input type="submit" name="Submit" value="Submit" onclick="check()" />
                </p>
              </form>

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                it's working fine for me... what do you expect that is not working?

                PS: I corrected some writing mistakes, though.... (syntax errors will show up in the Error Console)

                Comment

                • mukeshrasm
                  Contributor
                  • Nov 2007
                  • 254

                  #9
                  Originally posted by Dormilich
                  it's working fine for me... what do you expect that is not working?

                  PS: I corrected some writing mistakes, though.... (syntax errors will show up in the Error Console)
                  what are those corrections if you can please suggest me and how it could possible that it is working in your end but not in my.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    nothing special...
                    Code:
                    function check()
                    {
                        var a=document.getElementsByName("checkbox");
                        var j=0;
                            for (i=0, l=a.length; i<l; i++)
                            {
                                if (a[i].checked==true)
                                {
                                     j++;
                                }
                            }
                           if (j==0)
                           {
                              alert("please select checkbox")
                           }
                    // this is probably where you got no response
                     alert(j);
                    }

                    Comment

                    • mukeshrasm
                      Contributor
                      • Nov 2007
                      • 254

                      #11
                      Originally posted by Dormilich
                      nothing special...
                      Code:
                      function check()
                      {
                          var a=document.getElementsByName("checkbox");
                          var j=0;
                              for (i=0, l=a.length; i<l; i++)
                              {
                                  if (a[i].checked==true)
                                  {
                                       j++;
                                  }
                              }
                             if (j==0)
                             {
                                alert("please select checkbox")
                             }
                      // this is probably where you got no response
                       alert(j);
                      }
                      Yes!
                      Now it is working fine, but why I need to initializ twice in for loop

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        initializing twice??? which variable do you mean?

                        Comment

                        • mukeshrasm
                          Contributor
                          • Nov 2007
                          • 254

                          #13
                          Originally posted by Dormilich
                          initializing twice??? which variable do you mean?
                          Code:
                          for (i=0, l=a.length; i<l; i++)
                          cann't I use like I previous one
                          Code:
                          for(i=0;i<=a.length;i++)

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            of course you can, but the disadvantage is that you have to compute the array length every time anew, while the first one computes the length only once.

                            and another one: use count_var < array_length (as terminating condition), otherwise you'll access a non-existing element, which may cause your script to break

                            Comment

                            • mukeshrasm
                              Contributor
                              • Nov 2007
                              • 254

                              #15
                              Originally posted by Dormilich
                              of course you can, but the disadvantage is that you have to compute the array length every time anew, while the first one computes the length only once.

                              and another one: use count_var < array_length (as terminating condition), otherwise you'll access a non-existing element, which may cause your script to break
                              Thanks!

                              Actually, I was unware of this minute information.

                              Comment

                              Working...