I have some HTML like this:
<div id="Myclass">
<div class="Wrapper">
<input type="checkbox" class="SomeCheckboxClass">
<input type="checkbox" class="SomeCheckboxClass">
<input type="checkbox" class="SomeCheckboxClass">
</div>
<div class="Wrapper">
<input type="checkbox" class="SomeCheckboxClass">
<input type="checkbox" class="SomeCheckboxClass">
<input type="checkbox" class="SomeCheckboxClass">
</div>
</div>
I want to find the index of a the selected textbox, if any.
For now, I wrote this:
$('#MyClass .Wrapper').each(function () {
Index = 0;
$(this).find('.SomeCheckboxClass').each(function () {
if ($(this).attr('checked')) {
Index = $(this).index();
}
});
SomeFunction(Index);
});
Is there a better way to do this?
Thanks.
I have some HTML like this:
<div id="Myclass">
<div class="Wrapper">
<input type="checkbox" class="SomeCheckboxClass">
<input type="checkbox" class="SomeCheckboxClass">
<input type="checkbox" class="SomeCheckboxClass">
</div>
<div class="Wrapper">
<input type="checkbox" class="SomeCheckboxClass">
<input type="checkbox" class="SomeCheckboxClass">
<input type="checkbox" class="SomeCheckboxClass">
</div>
</div>
I want to find the index of a the selected textbox, if any.
For now, I wrote this:
$('#MyClass .Wrapper').each(function () {
Index = 0;
$(this).find('.SomeCheckboxClass').each(function () {
if ($(this).attr('checked')) {
Index = $(this).index();
}
});
SomeFunction(Index);
});
Is there a better way to do this?
Thanks.
Share Improve this question asked Jun 6, 2012 at 15:50 frenchiefrenchie 52.1k117 gold badges320 silver badges527 bronze badges 8- What if more than one checkbox is checked? – James Allardice Commented Jun 6, 2012 at 15:53
-
the
[checked]
attribute isn't the indicator of whether a checkbox is checked, also i think my answer to this question is relevant. – zzzzBov Commented Jun 6, 2012 at 15:55 - @JamesAllardice: only one checkbox is selected; I've got another function that makes sure of that. – frenchie Commented Jun 6, 2012 at 15:57
- 1 @frenchie, why are you using checkboxes if only one can be selected? You should be using radio buttons. – zzzzBov Commented Jun 6, 2012 at 15:59
- @zzzzBov: because I want to be able to unselect them all. With radio buttons, once you select one, you can't revert to the state "none selected". – frenchie Commented Jun 6, 2012 at 17:21
3 Answers
Reset to default 5You can use .map
: http://jsfiddle/p4nD7/1/.
$("#Myclass .Wrapper :checkbox:checked").map(function() {
return $(this).index(); // index in parent
}).each(function() {
"use strict"; // no object for `this` but just the primitive value
SomeFunction(this);
});
$('#Myclass .Wrapper').each(function() {
var Index = $('.SomeCheckboxClass:checked', this).index();
SomeFunction(Index);
});
DEMO
You can also use map()
$('#Myclass .Wrapper').map(function() {
var Index = $('.SomeCheckboxClass:checked', this).index();
SomeFunction(Index);
});
DEMO
Why not just do this:
$('.checkbox').click(function() {
if($(this).attr('checked')) {
SomeFunction($(this).index());
}
});