help me with this javascript function to check items on checkboxlist

the first option is working, i can check all, and uncheck all, but what about if i want to select only half of it, and other half.

this code i did is not working for check half. can u help me
<script type="text/javascript">
    function Select(Type, Select, checkBoxListID) {
        var cph = document.getElementById("ContentPlaceHolder1_Panel7");
        var inputs = cph.getElementsByTagName("input");
        var inputId = "";
        if (Type != 1) { 
            for (var i = 0; i < inputs.length; i++) {
                inputId = inputs[i].id;
                if (inputId.indexOf(checkBoxListID) != -1) {
                    inputs[i].checked = Select;
                }
            }
        }
        if (Type != 2) {
            for (var i = 0; i < inputs.length - 40; i++) {
                inputId = inputs[i].id;
                if (inputId.indexOf(checkBoxListID) != -1) {
                    inputs[i].checked = Select;
                }
            }
        }
        if (Type != 3) {
            for (var i = 30; i < inputs.length; i++) {
                inputId = inputs[i].id;
                if (inputId.indexOf(checkBoxListID) != -1) {
                    inputs[i].checked = Select;
                }
            }
        }
    }
</script>

Open in new window

LVL 1
rafaelrglAsked:
Who is Participating?
 
Tom BeckConnect With a Mentor Commented:
Does this get you what you want?

<a href="#" onclick="javascript:Select(2, true, 'CheckBoxList1')">First Half</a> | <a href="#" onclick="javascript:Select(3, true, 'CheckBoxList1')">Second Half</a> | <a href="#" onclick="javascript:Select(1, false, 'CheckBoxList1')">Nenhum</a>
<script type="text/javascript">
 function Select(Type, check, checkBoxListID){ 
    var cph = document.getElementById("ContentPlaceholder1");
    var inputs = cph.getElementsByTagName("input"); 
    var inputId = "";      
    for (var i = 0; i < inputs.length; i++) {
        inputId = inputs[i].id;
        if(inputId.indexOf(checkBoxListID) != -1) {
            switch (Type) {
                case 1:
                    inputs[i].checked = check;
                    break;
                case 2:
                    if(i < inputs.length / 2)inputs[i].checked = check;
                    break;
                case 3:
                    if(i > inputs.length / 2)inputs[i].checked = check;
                    break;
            }
        }
  }
}
</script>

Open in new window

0
 
MacAnthonyCommented:
It's bad form and confusing to name a parameter and your function the same. How many inputs are there? Block 3 will only work if there are fewer than 30. Also keep in mind if Type is 2, then the first block and last block will run and they seem to be doing similar things.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.