2

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

3
  • Where are the other checkboxes? What control are you using to group the checkboxes? Can you use jQuery? Commented Jun 15, 2010 at 19:43
  • I am suing jquery but I am not getting the all the checkbox ids.. in IE8.. but i am getting in Firefox.. Commented Jun 15, 2010 at 19:45
  • same input checkbox generates with differnt id's.. Commented Jun 15, 2010 at 19:45

4 Answers 4

4

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")
Sign up to request clarification or add additional context in comments.

Comments

2

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?

Comments

1

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"); 

4 Comments

This si not working in IE8? var checks= $("input[@type=checkbox]:checked");
@type? I think the @ was left behind in jQuery 1.2.6. :o)
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.
0

Please try this:

  var checkboxes = new Array();

    var $checkboxCtrls  = $('input[type=checkbox]');

    $.each($checkboxCtrls,function(i,ctrl){

        checkboxes.push($(this).attr('id'));

    });

Fiddle link for Assistance : http://jsfiddle.net/gJeCT/1/

  • Note: for IE, comment the console logging statement and use alert(), as console logging is supported in all browsers except IE versions.

Comments

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.