0

I am having checkbox in each itemTemplate of asp:gridview

I want to get ids or values of those many selected checkboxes using only javascript

1
  • Can you post some code so we can see what HTML you are generating. Thanks Commented May 12, 2011 at 10:44

2 Answers 2

1

In pure javascript I'm not sure about platform portability: you'd REALLY want jQuery or some other helper library here.

With jQuery:

var values = [];
var ids = [];
jQuery.each(jQuery("input:checkbox").find(":checked"), function(){
values.push(jQuery(this).val());
ids.push(jQuery(this).attr("id");
});

will give you the ids and values of all the checked checkboxes.

EDIT: ugly, but this might work...

var values = [];
var ids = [];
var inputs = document.getElementsByTagName("input");
var i;
for(i=0;i<inputs.length;i++){
  if(inputs[i].hasAttributes() && inputs.getAttribute('type') == "checkbox" && inputs.getAttribute('checked')){
     values.push(inputs[i].getAttribute('value'));
     ids.push(inputs[i].getAttribute('id'));
  }
}

Let me know if that does what you want.

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

1 Comment

i am knowing jQuery, but here i want it in only javascript
0

I am not exactly sure on what you are trying to do but this might help you out. This will get all of the inputs on the screen and process only the checked ones.

var inputList = document.getElementsByTagName("input");
var resultsArray = [];

for(var i = 0; i < inputList.length; i++) {
    if (inputList[i].getAttribute("checked") == true) {
        resultsArray.push(inputList[i]);
    }
}

Sorry, forgot to tell you that this would be a list of elements. You will then need to extract them however you want to from resultsArray.

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.