0

I am using the following code to check if at least one checkbox is selected from my checkboxlist in asp.net. I am using jGrowl to throw up a message which works, but the message is still displayed if a selection is made. Any ideas? Also, story_type is an asp label, I am using the code below, but I can't get the jGrowl message to display...

var story_type = document.getElementById('story_type').value;
var agile_list = document.getElementById('agile_factors');
var arrayOfCheckBoxes = agile_list.getElementsByTagName("input"); 

for (counter = 0; counter < arrayOfCheckBoxes.length; counter++) {

    if (arrayOfCheckBoxes[counter].checked) {

        return true;

    } else {

        (function($) {
         $.jGrowl("Please Choose up to 3 Agile Factors", { theme: 'smoke', closer: true });
        })(jQuery);

        return false;
    }

}

if (story_type == "[SELECT TYPE]") {
    (function($) {
        $.jGrowl("Please Select Story Type", { theme: 'smoke', sticky: true, closer: true })
        return false;

    })(jQuery);

    return true;
} 
1
  • Are the checkboxes the only 'input' elements on the page? Commented Jan 29, 2011 at 1:12

1 Answer 1

1

Since you have jQuery you can do this to simplify your code a bit:

   <div id="options">
        <input type="checkbox" name="opt1" /> Option 1
        <input type="checkbox" name="opt2" /> Option 2
        <input type="checkbox" name="opt3" /> Option 3
        <input type="checkbox" name="opt4" /> Option 4
    </div>
    <div>
        <a href="#" id="validate">validate</a>
    </div>
    <script type="text/javascript">
    ;(function() {

        $("#validate").click(function() {
            validateInputs();
        });


        function validateInputs() {
            alert($("#options input:checked").length + " checked");
        }
    })();

</script>

Here is a jsFiddle of the above code; http://jsfiddle.net/rvXHG/

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly that. Is that a typo on the line ;(function(){ ... - ; should be a dollar $?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.