I'm trying to check whether or not all the visible check boxes in a certain series are checked and i thought of just counting those that are visible and those that are visible and checked to see if the numbers are the same. The problem is I can't get the visible nor the checked selectors to work.
These are some of the ideas I had but didn't work:
if($j("input[id^='chk_camp']:visible:checked").length == $j("input[id^='chk_camp']:visible").length)
both sides are 0 in this case
if($j("input[id^='chk_camp']").filter(':visible').filter(':checked').length == $j("input[id^='chk_camp']").filter(':visible').length)
also returned 0 on both sides.
Also tried
if($j("input[id^='chk_camp'][visible][checked]").length == $j("input[id^='chk_camp'][visible]").length)
and this also returns 0 on both sides.
As a note $j("input[id^='chk_camp']").length
returns the correct value. Also the browser I'm working with is Firefox.
What am I doing wrong here?
Answer: Aparently what I'm doing wrong is somewhere else. I was doing these checks before actually making the div containing the checkboxes visible so all the visibility checks were returning false.
prop() and is() method are the two way by which we can check whether a checkbox is checked in jQuery or not. prop(): This method provides an simple way to track down the status of checkboxes. It works well in every condition because every checkbox has checked property which specifies its checked or unchecked status.
To check whether a Checkbox has been checked, in jQuery, you can simply select the element, get its underlying object, instead of the jQuery object ( [0] ) and use the built-in checked property: let isChecked = $('#takenBefore')[0]. checked console. log(isChecked);
This works fine for me.
$(".inputClass:checked:visible");
$(".inputClass:checked:visible").length;
OR adapting the above answer.
jsfiddle
$('input:visible:checked').each(function() {
$(this).wrap('<div />');
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With