S.P.
S.P.

Reputation: 369

jQuery check if checkbox is unchecked not working

I got some problem while checking checkbox, it always return true, didn't work in the false statement, I want to stop before click the add button if none of the checkbox is checked. How to fix it?

Here's my code:

$('.add').click(function() {
	var weekDays = [];
	$('#checkBox :checked').each(function () {
		weekDays.push($(this).attr('name'));
	});
	
    var row = '<tr>'
			+ '<td class="rowDays">' + weekDays + '</td>'
			+ '</tr>';
	
    if (!$("#days").find("checkbox").is(":checked")) {
   		$(row).insertAfter($('#days > tbody > tr:last'));
	} else{
    	alert('unchecked!')
        return false;
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<table id="checkBox">
    <tbody>
	    <tr>
		    <th>Sun</th>
			<th>Mon</th>
			<th>Tue</th>
			<th>Wed</th>
			<th>Thu</th>
			<th>Fri</th>
			<th>Sat</th>
        </tr>
		<tr>
			<td><input name="Sunday" type="checkbox" value="0"></td>
			<td><input name="Monday" type="checkbox" value="1"></td>
			<td><input name="Tuesday" type="checkbox" value="2"></td>
			<td><input name="Wednesday" type="checkbox" value="3"></td>
			<td><input name="Thursday" type="checkbox" value="4"></td>
			<td><input name="Friday" type="checkbox" value="5"></td>
			<td><input name="Saturday" type="checkbox" value="6"></td>
		</tr>
	</tbody>
</table>
            
<button type="button" class="add">Add Days</button>
<br>
<br>
<table id="days">
    <tbody>
		<tr>
			<th>Days</th>
		</tr>
	</tbody>
</table>

JSFiddle

Upvotes: 0

Views: 522

Answers (3)

billyonecan
billyonecan

Reputation: 20270

Your condition is incorrect $("#days").find("checkbox") will not find anything (#days doesn't have any checkboxes, and checkbox isn't a valid selector for retrieving checkboxes).

You just need to test check the length of the checked checkboxes in #checkBox:

if (! $('#checkBox :checked').length ) {
    return false;
}

Here's a complete example:

$('.add').click(function() {

    if (! $('#checkBox :checked').length ) {
        return false;   
    }

    var weekDays = $('#checkBox :checked').map(function() { 
        return this.name;
    }).get();

    var row = '<tr>'
            + '<td class="rowDays">' + weekDays.join(', ') + '</td>'
            + '</tr>';

    $(row).appendTo('#days > tbody');

});

Here's a fiddle

Upvotes: 1

Juvilnoz
Juvilnoz

Reputation: 46

UPDATED

1) Set a common class (Ex: myCheckbox) to each input as :

<input class="myCheckbox" name="Sunday" type="checkbox" value="0">

2) Get all input which have this class and which is 'checked'

3) Use the 'length' property as :

if ($("input[class=myCheckbox]:checked").length > 0) {
    $(row).insertAfter($('#days > tbody > tr:last'));
} else{
    alert('unchecked!')
    return false;
}

Upvotes: 2

void
void

Reputation: 36703

if ($("#days").find("checkbox").is(":checked").length!=0) {
        $(row).insertAfter($('#days > tbody > tr:last'));
} else{
        alert('unchecked!')
        return false;
}

Use $("#days").find("checkbox").is(":checked").length to calculate the number of checked elemented and then compare it with 0.

Fiddle

Upvotes: 0

Related Questions