views:

36

answers:

1

Using asp.net to generate several checkboxes. Looking for a javascript solution that will test any checkbox starting with the name pdf* to see if more than 5 are checked, then alert over that number.

There are other checkboxes I do not want this to run on that do not shae the name pdf*.

Thanks in advance.

here is an example of my checkboxs:

<td><input type="checkbox" name="pdf14" value="2250"></td>

<td><input type="checkbox" name="pdf15" value="2251"></td>

<td><input type="checkbox" name="pdf16" value="2252"></td>

<td><input type="checkbox" name="print" value="2253"></td> 

<<

+1  A: 
var inputs = document.getElementsByTagName("input");
var numSelected = 0;
var current;

for (var i=inputs.length;i--;) {
    current = inputs[i];

    if (current.type.toLowerCase() == "checkbox" && current.checked && current.name.indexOf("pdf") == 0) {
        current++;
    };
};

if (current > 5) {
    alert("You've got " + current + " selected! :(");
}

If you happened to use jQuery, it'd be as easy as

var selectedPdfCheckboxes = $('input:checkbox[name^=pdf]');

if (selectedPdfCheckboxes.length > 5) {
    alert("You've got " + selectedPdfCheckboxes.length + " selected! :(");
}

Edit:

The jQuery selector I've used (^=) reads as "start with", so no * equivalent is needed.

The jQuery code will execute wherever it is placed, however it must be executed no sooner than document.ready(), to ensure the elements you are selecting are present in the DOM. If you place it inside document.ready, it will fire as soon the document ready event is fired. You might want to place the code inside an event handler for another event.. such as the submission of a form?

$(document).ready(function () {
    $("#aForm").bind('submit', function (e) {
        var selectedPdfCheckboxes = $('input:checkbox[name^=pdf]');

        if (selectedPdfCheckboxes.length > 5) {
            alert("You've got " + selectedPdfCheckboxes.length + " selected! :(");

            e.preventDefault();        
        }
    });
});
Matt
nice, but even though it is in my page it doesn't seem to fire. i am trying the jQuery solution. should I add pdf*? is the * a wildcard in jQuery?
James Campbell
R. Hill
@ r. Hill I tried that got: Webpage error detailsUser Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) ; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; InfoPath.3; Media Center PC 6.0; .NET4.0C; .NET4.0E; MS-RTC LM 8)Timestamp: Tue, 20 Jul 2010 17:59:34 UTCMessage: Invalid argument.Line: 11Char: 1Code: 0
James Campbell
The original answer should work well, either the plain ol' javascript or the jquery method. If it's not firing as you say, where are you running it? If it's just in the global js scope, then it would try to execute before any of your <input> elements were added to the page. Is your js code in a function that gets called later? Or in a `$(document).ready(function() { /* code */ } );` block?
Samuel Meacham
I tried it in the $(document).ready(function() and it fires as soon as the load happens and I get the alert without checking boxes. When I take it out, it never fires no matter how many I check.
James Campbell
@Vecdid: see my updated answer :)
Matt
I got it to work, thank you
James Campbell