views:

61

answers:

3

I have input checkbox something like this..

 <input class="inputchbox" id="Pchk" type="checkbox" name="check" value="<%=Model.ID%>" />

using javascript I need to get the all checkbox checked Ids in to the array?

how do I need to get?

thanks

A: 

Something like this should work.

var inputs = document.getElementsByTagName("input"); 
var checks = [];
for (var i = 0; i < inputs.length; i++) {  
  if (inputs[i].type == "checkbox" && inputs[i].checked) {  
    checks .push(inputs[i]);  
  }  
}  

using jQuery you could do this:

var checks= $("input:checkbox:checked"); 
Dustin Laine
This si not working in IE8?var checks= $("input[@type=checkbox]:checked");
@durilai - `@type`? I think the `@` was left behind in jQuery 1.2.6. :o)
patrick dw
what I need to use?
I used the code which Durilai sujjested in Friefox I am getting all values but in IE I am getting only Frist Id value other id value are zero its showing
@patrick - good catch, old code. Updated to use jQuerys latest and greatest.
Dustin Laine
+3  A: 

Pure Javascript answer

var formelements = document.forms["your form name"].elements;
var checkedElements = new Array();
for (var i = 0, element; element = formelements[i]; i++) {
  if (element.type == "checkbox" && element.checked) [
    checkedElements.push(element);
  }
}

Jquery Answer

$("input:checked")
John Hartsock
+2  A: 

Does this look like a solution to your issue? Parse page for checkboxes via javascript

Basically, you'd get the element for your <input type="checkbox"> tag and verify whether or not the checked attribute evaluates to true.

Note: I'm unsure if my response should be a comment or an answer -- this almost looks like a duplicate question?

btlachance