Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

help me with this javascript function to check items on checkboxlist

Posted on 2011-10-29
2
Medium Priority
?
218 Views
Last Modified: 2012-05-12
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

0
Comment
Question by:rafaelrgl
2 Comments
 
LVL 11

Expert Comment

by:MacAnthony
ID: 37051646
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
 
LVL 38

Accepted Solution

by:
Tom Beck earned 2000 total points
ID: 37051680
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

jQuery is a JavaScript library that greatly simplifies JavaScript programming. AJAX is an acronym formed from "Asynchronous JavaScript and XML."  AJAX refers to any communication between client and server, when the human client does not observe a…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month21 days, 5 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question