Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Jun 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    to check the consecutive checkbox

    Hi ,




    plz do help me for checking the consecutive checkboxes in a group of checkbox using if loop.



    Regerds,
    vamshi

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,732
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by vamshi View Post
    plz do help me for checking the consecutive checkboxes in a group of checkbox using if loop.
    I don't really understand your question - please be more specific.

    This example may or may not be what you are wanting.


    Code:
    <form name = "myform">
    RED <input type = "checkbox" name = "box" value =  "red">
    BLUE <input type = "checkbox" name = "box" value =  "blue">
    GREEN <input type = "checkbox" name = "box" value =  "green">
    YELLOW <input type = "checkbox" name = "box" balue = "yellow">
    <br><br>
    <input type = "button" value = "Boxes Checked?" onclick = "CheckBoxes()"
    </form>
    
    <script type = "text/javascript">
    
    function CheckBoxes(){
    var bool = false;
    var count = 0;	
    for (var i=0;i<document.myform.box.length;i++)	{
    if (document.myform.box[i].checked)		{
    bool = true;			
    count++;	
    }
    }	
    alert((bool)?"You've checked at least one box.":"You haven't checked any boxes!");
    alert ("You have checked " + count + " boxes")
    }
    </script>
    Another example:-

    Code:
    <form name = "myform">
    <input name = "myname" type = "checkbox" value = "Red"> RED <br>
    <input name = "myname" type = "checkbox" value = "Blue"> BLUE <br>
    <input name = "myname" type = "checkbox" value = "Green"> GREEN <br>
    <input name = "myname" type = "checkbox" value = "Yellow"> YELLOW<br><br>
    <input type = "button" value = "Get Color Choices" onclick="cboxlist()"><br>
    </form>
    
    <script type = "text/javascript">
    
    function cboxlist(){
    var choice = "Your colour choice is: ";
    var x = new Array();
    var colors = document.myform.myname;
    for ( i = 0; i < colors.length; i++ ) {
    if ( colors[i].checked ) {
    x.push(colors[i].value);
    }
    }
     alert (choice + (x.length == 0 ? 'NONE' : x.join(' | ')));
    } 
    
    </script>
    Or do you want a code to check/uncheck all the checkboxes in a group when a button is clicked???

    Code:
    <script type = "text/javascript">
    var checked = false;
    function checkedAll () {
    checked == true? checked = false:checked = true;
    var els = document.getElementById("mydiv").getElementsByTagName("input");
    var len = els.length;
    for (var i = 0; i < len; i++) {
    els[i].checked = checked;
    }
    }
    </script>
    </head>
    
    <body>
    <form id="myform">
    <div id = "mydiv">
    <input type="checkbox" name="chk1">
    <input type="checkbox" name="chk2">
    <input type="checkbox" name="chk3">
    </div>
    <br>
    Check/Uncheck All: <input type="checkbox" name="checkall" onclick="checkedAll();">
    </form>
    Seen in the classified ads: Nice parachute - never opened - used once.
    Last edited by Philip M; 06-29-2012 at 08:22 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •