Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: how can I select only the checkboxes that are visible and checked?

Tags:

jquery

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.

like image 882
Bogdan Avatar asked May 20 '13 08:05

Bogdan


People also ask

How do you check is the specific checkbox checked or not?

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.

How do you check all checkboxes are checked or not in jQuery?

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


1 Answers

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 />');
});
like image 154
Vladimir Ilyich Ulyanov Avatar answered Oct 10 '22 21:10

Vladimir Ilyich Ulyanov