Reputation: 3
Here is my problem...I have a page that loads a list of clients and on click of there name, a popup comes up and you can define what time they are allowed to login, here is the html code.
<form action="" method="post" name="access_hours">
<table width="100%">
<tr>
<td colspan="5">
<input type="checkbox" id="all" name="107" /> Check/Uncheck all
</td>
</tr>
<div id="check_107">
<tr>
<td><input type="checkbox" name="accesstimes[]" id="107" value="01" />1AM</td>
<td><input type="checkbox" name="accesstimes[]" id="107" value="02" /> 2AM</td>
<td><input type="checkbox" name="accesstimes[]" id="107" value="03" /> 3AM</td>
<td><input type="checkbox" name="accesstimes[]" id="107" value="04" /> 4AM</td>
<td><input type="checkbox" name="accesstimes[]" id="107" value="05" /> 5AM</td>
</tr>
<tr>
<td><input type="checkbox" name="accesstimes[]" id="107" value="06" /> 6AM</td>
<td><input type="checkbox" name="accesstimes[]" id="107" value="07" /> 7AM</td>
<td><input type="checkbox" name="accesstimes[]" id="107" value="08" /> 8AM</td>
<td><input type="checkbox" name="accesstimes[]" id="107" value="09" /> 9AM</td>
<td><input type="checkbox" name="accesstimes[]" id="107" value="10" />10AM</td>
</tr>
<tr>
<td><input type="checkbox" name="accesstimes[]" id="107" value="11" />11AM</td>
<td><input type="checkbox" name="accesstimes[]" id="107" value="12" />12PM</td>
<td><input type="checkbox" name="accesstimes[]" id="107" value="13" /> 1PM</td>
<td><input type="checkbox" name="accesstimes[]" id="107" value="14" /> 2PM</td>
<td><input type="checkbox" name="accesstimes[]" id="107" value="15" /> 3PM</td>
</tr>
<tr>
<td><input type="checkbox" name="accesstimes[]" id="107" value="16" /> 4PM</td>
<td><input type="checkbox" name="accesstimes[]" id="107" value="17" /> 5PM</td>
<td><input type="checkbox" name="accesstimes[]" id="107" value="18" /> 6PM</td>
<td><input type="checkbox" name="accesstimes[]" id="107" value="19" /> 7PM</td>
<td><input type="checkbox" name="accesstimes[]" id="107" value="20" /> 8PM</td>
</tr>
<tr>
<td><input type="checkbox" name="accesstimes[]" id="107" value="21" /> 9PM</td>
<td><input type="checkbox" name="accesstimes[]" id="107" value="22" />10PM</td>
<td><input type="checkbox" name="accesstimes[]" id="107" value="23" />11PM</td>
<td><input type="checkbox" name="accesstimes[]" id="107" value="00" />12AM</td>
<td><input type="checkbox" name="accesstimes[]" id="107" value="NONE" />NONE</td>
<td> </td>
</tr>
</div>
<td colspan="5">
<input type="hidden" name="id" value="107" />
<input type="submit" class="updatebutton" name="confirm" value="Update" />
</td></tr></table>
</form>
</div>
The issue is, there are about 10 sets of these blocks on the page due to the jquery popup, and they all are under a div with the id being "check_(idofblock)" which isn't a problem. I have the check/uncheck all box working. Now what i can't seem to figure out, how do i get it so when any checkboxes are selected, if I click on the one marked "NONE" then that's the only one selected as well as if NONE is selected, how do i uncheck it after i click on anything else? I just can't seem to get it right. Here is my code for the "check/uncheck all" which checks all except for NONE.
$('input[id=all]').click(function() {
the_id = $(this).attr('name');
$('input:checkbox[id="'+the_id+'"]').attr('checked', ($(this).is(':checked')));
$('input:checkbox[id="'+the_id+'"][value="NONE"]').attr('checked', false);
});
I don't have any code to post for what I need as it was racking my brain so I deleted it and figured i'd ask everyone here who is way smarter with jquery then I am.
Any help would be greatly appreciated :)
Upvotes: 0
Views: 1804
Reputation: 2856
first of all it's so wrong to use the same id on the page. why don't you don't trick with jquery?
instead of:
<input type="checkbox" name="accesstimes[]" id="107" value="21" />
by
<input type="checkbox" name="accesstimes[]" element_id="107" value="21" />
and
<input type="checkbox" name="accesstimes[]" id="107" value="NONE" />
by
<input type="checkbox" name="accesstimes[]" element_id="107" class="NONE" />
and in the end do:
$('#all').click(function() {
the_id = $(this).attr('name');
$('input:checkbox[element_id="'+the_id+'"]').prop('checked', ($(this).is(':checked')));
$('.NONE').prop('checked', false);
});
it works:
EDIT: I also implemented the checkbox none for you ;)
http://jsfiddle.net/workdreamer/LGnff/
Upvotes: 1
Reputation: 31043
not an elegant solution but it works and as @ridecar2 suggested using the identical ids is not a good practice/ should be avoided
$('input[id=all]').click(function() {
the_id = $(this).attr('name');
$('input:checkbox[id="'+the_id+'"]').attr('checked', ($(this).is(':checked')));
$('input:checkbox[id="'+the_id+'"][value="NONE"]').attr('checked', false);
});
$(":checkbox").filter(function(){
return $(this).closest("td").text()!='NONE';
}).click(function(){
$("td").filter(function(){
return $(this).text()=='NONE'}).find(":checkbox").prop("checked",false);
});
$("td").filter(function(){
return $(this).text()=='NONE'}).find(":checkbox").click(function(e){
$(":checkbox").not(this).prop("checked",false);
});
Upvotes: 0
Reputation: 19345
#
- not the attribute selectordiv
is not a valid child element to a table
.That being said, if the user clicks a checkbox with name="107"
, you can uncheck all the checkboxes inside a div element with id check_107
like this, assuming you change from id="all"
to class="checkAll"
$('.checkAll').click(function() {
var id = $(this).attr("name");
$('#check_' + id + ' input[type=checkbox]').removeAttr("checked");
});
Upvotes: 0