i have a checkbox who have class= check how i can get id of all checkbox in javascript
A:
If you want any hope of cross browser compatibility, you use a javascript framework (like jquery)
Matt Briggs
2010-09-11 07:27:24
I have yet to see a solution here that is cross browser compatible. Thanks for the down votes guys.
Matt Briggs
2010-09-12 17:54:23
Just FYI, I didn't downvote any of these, even though somebody did downvote mine >_> But just wondering, how is my solution not cross-browser compatible? In case you're wondering why you have -3, it's because you're just writing "you can't do it". Perhaps next time you can provide some code on how you would do it even if you use jQuery. I mean, if you just load jQuery, it does not automatically do anything.
Kranu
2010-10-15 07:25:05
@Kranu: The question didn't mention jquery, the origional poster aparently happens to use it, but that was just lucky for you. What I was saying is that there isn't any way to do this in just javascript that will work on all browsers, unless you already have an abstraction layer of some sort. I got voted down 3 times, even though that is correct -- your answer was for a specific abstraction layer, and the other one wont work in IE.
Matt Briggs
2010-10-15 12:48:58
@Kranu: As for the down vote, people who don't use jquery tend to be irritated here when people just assume that everyone is using it. I don't down vote for that, but I have seen it done many times. Anyways, im just going to add the jquery to the question, hopefully you won't get any more downvotes
Matt Briggs
2010-10-15 12:52:34
I think people are just pretty grouchy about this question in general. If you look, the question and all of the answers are all downvoted. But the reason why you got the most extreme down votes was because you didn't exactly answer the question. You just said you use blah blah blah.
Kranu
2010-10-16 00:08:08
A:
Perhaps you could clarify which elements you want. If you want all checkboxes, use the example below. Otherwise, if you want only elements with the .check class, replace $('input[type=checkbox]') in my example with $('.check')
The easiest way to do this would be to utilize jQuery. If you just want the ID's, this will store each of them in the array "checkid":
var checkid=new Array();
$('input[type=checkbox]').each(function() {
checkid[checkid.length]=$(this).attr('id');
});
Kranu
2010-09-11 07:31:14
Neither `$` nor `Array.prototype.push` are standard and cross-browser compatible. Being that the question doesn't specify a library, I don't think using jQuery/CSS selector syntax is necessarily appropriate.
eyelidlessness
2010-09-11 08:14:40
I'm sure that push is compatible with the popular browsers. However, to maximize compatibility, I've changed the code slightly. As for the jQuery $, I think you need to get your facts straight, because jQuery is cross-browser compatible. If you don't think using a CSS selector is appropriate, what do you suggest? I am using jQuery in my example because jQuery is designed to be cross-browser compatible, and you don't have to deal with all the little details and glitches of some browsers.
Kranu
2010-09-12 07:49:09
A:
Summing up all suggestions, you would end with this code snippet
var inputs = document.getElementsByTagName("input"),
checked_ids = [],
input = null,
i = inputs.length;
while (i--) {
input = inputs[i];
if (input.type === "checkbox" && input.className === "check" && input.checked === true)
checked_ids.push(input.id);
}
Edit
Revised code
Thanks for down-voting without an explanation...
john_doe
2010-09-12 08:00:06