all checked when first checkbox checked - php

i have a problem with checkbox . i want to check all when the first checkbox is checked. this is my code
<tr>
<td><label for="">Pilih Pendidikan</label></td>
<td style="text-align:left;">
<input type="checkbox" id="pilih_semua_pdk">Pilih Semua
<?php foreach ($query2 as $row2) {?>
<input class="form-control required check_pdk" type="checkbox" name="pendidikan[]" value="<?php echo $row2[id_pendidikan]; ?>"
<?php
$pendidikan = $_POST[pendidikan];
if ($pendidikan !=NULL) {
foreach($pendidikan as $pendidikan){
if ($pendidikan == $row2[id_pendidikan]) {
echo "checked";
}
}
}
?>
><?php echo $row2[nama_pendidikan]; ?>
<?php } ?>
</td>
</tr>
i try this code but doesnt work
<script>
$("#pilih_semua_pdk").click(function () {
$(".check_pdk").prop('checked', $(this).prop('checked'));
</script>

Try this, you need to bind an click event on first checkbox. On click of the first checkbox check if that is checked!. If yes then find all checkbox to get going.
$('#pilih_semua_pdk').on('click', function(){
if( $(this).is(':checked'))
{
//find parent td and its all checkboxes excluding first
$(this).parent().find('input[type=checkbox]').not('#pilih_semua_pdk').each(function(){
$(this).prop('checked', true);
});
}
});

jQuery makes this a piece of cake.
$(document).ready(function(){
// DOM has been loaded, attach click event for first checkbox
$('#pilih_semua_pdk').on('click', function(){
// verify if it is checked
if($(this).is(':checked'))
{
// check/tick the remaining checkboxes of the same td
$(this).siblings('.check_pdk').prop('checked', 'checked');
}
else
{
// do something when the first checkbox is unchecked
}
});
});

Related

Check and uncheck the checkbox by row

Good Day all...I'm having a multiple row which is fetched from my DB by foreach loop.
And I have 2 checkboxes in each row, What I'm trying is... If I check the 1st checkbox mean automatically the 2nd checkbox need to check.
For that I'm using ID to select that 2nd checkbox, Now because of that id is same for every row, if I select
1st row ,1st checkbox mean it's selecting all the second check box. But what I need is to get that particular selected row's 2nd checkbox need to be checked.
Anyone Help. Sorry for not sharing any live demo like jsfiddle. I hope u guys understand my problem.
<thead>
<th><input type="checkbox" id="select_all"></th>
</thead>
<?php foreach ($category_details as $key => $category_detail): ?>
<tr>
<td>
<input type="checkbox" class="checkbox" id="select_img" name="ids[]" value="<?php echo $category_detail['id'];?>"/>
<input type="checkbox" class="checkbox checkimg" name="imgs[]" value="<?php echo $category_detail['category_image'];?>"/>
</td>
<td>...</td> <!-- etc -->
</tr>
<?php endforeach ?>
$(document).ready(function(){
$('#select_all').on('click',function(){
if(this.checked){
$('.checkbox').each(function(){
this.checked = true;
});
}else{
$('.checkbox').each(function(){
this.checked = false;
});
}
});
$('#select_img').on('click',function(){
if(this.checked){
$('.checkimg').each(function(){
this.checked = true;
});
}else{
$('.checkimg').each(function(){
this.checked = false;
});
}
});
$('.checkbox').on('click',function(){
if($('.checkbox:checked').length == $('.checkbox').length){
$('#select_all').prop('checked',true);
}else{
$('#select_all').prop('checked',false);
}
});
});
You can use siblings jquery function. For example:
$('.whole_row').on('click',function(){
if(this.checked){
$(this).siblings().each(function(index, checkbox){
checkbox.checked = true;
});
}else{
$(this).siblings().each(function(index, checkbox){
checkbox.checked = false;
});
}
});
this way it will iterate the siblings tags and won't go outer the table rows. Just use classes not ids
Edit:
just a style note
$('.whole_row').on('click',function(){
let check = this.checked;
$(this).siblings().each(function(index, checkbox){
checkbox.checked = check;
});
});
html like this:
<tr>
<input type="checkbox" class="whole_row">one
<input type="checkbox">two
<input type="checkbox">three
<input type="checkbox">four
</tr>

JQuery - checkboxes dont't turn on

I'm having a problem with jquery.
I have a checkbox with ID "#checkAll" and checkboxes with class "checkboxes".
I'd like to turn all checkboxes on when the checkbox with ID "checkAll" is checked,
and I'd like to turn all checkboxes off when the checkbox with ID "checkAll" is checked off.
But, What's having now is that when I first click the checkbox(#checkAll), all other checkboxes are turned on and when I first turn the ckeckbox(#checkAll), all other checkboxs are turned off as well.
But, when I try to click the checkbox(#checkAll) in the second time, all other checkboxes are not turned on.
I don't know why this happens.
Please help me out!
This is my javascript handling events.
ADMIN.event = (function() {
function _init(){
$(function(){
var checkAll = $('#checkAll'),
checkboxes = $('.checkboxes');
checkAll.click(function(){
if($(this).is(':checked')){
checkboxes.attr('checked', 'checked');
} else {
checkboxes.removeAttr('checked');
}
})
});
}
_init();
}());
This is my view file in php.
<table class="fullTable">
<tr class="listTableTr">
<th class="listTableTh tinyTh"><input type="checkbox" name="" id="checkAll" /></th>
<th id="articleTitleTh" class="listTableTh">title</th>
</tr>
<?php foreach($postData as $post): ?>
<td class="listTableTd"><input type="checkbox" name="" class="checkboxes" /></td>
<td class="listTableTd"><?php echo $post['title'];?></td>
<?php endforeach; ?>
</table>
Try with .prop() like
checkAll.click(function(){
checkboxes.prop('checked',this.checked);
});
And its better to use even with .on() like
checkAll.on('click' , function(){
checkboxes.prop('checked',this.checked);
});
Try this :
$('#checkAll').on('click',function(){
if($("#checkAll").is(':checked')) {
$('.checkboxes').attr('checked',true);
} else {
$('.checkboxes').attr('checked',false);
}
});
DEMO
Try this
$("#checkAll").click(function () {
if ($(this).is(":checked"))
$(".checkboxes").prop("checked","checked");
else
$(".checkboxes").prop("checked",false);
});

How to add new content when a checkbox is clicked?

I made a design with ajax, jquery, mysql and checkbox in php. It is like that :
There is box with checkboxes. And there is another box which is empty. When you checked a checkbox in first box, information about that checkbox will be displayed in empty box. I did it and work good.But when you clicked another checkbox, information about first you clicked dissappear and only current selection is displayed. That is to say, I want to add new selection on old ones. Also how can I delete information in second box when I unchecked a checkbox. Thank you.
Here is HTML code :
<div id="first_box">
<label class='checkbox'>
<input type='checkbox' id=1>
First checkbox
</label>
<label class='checkbox'>
<input type='checkbox' id=2>
Second checkbox
</label>
<label class='checkbox'>
<input type='checkbox' id=3>
Third checkbox
</label>
</div>
<div id="second_box"> </div>
Here is jquery code :
$(document).ready(function() {
$("#first_box input:checkbox").change(function() {
if($(this).is(":checked")) {
$.post("/here/is/url", { strID:$(this).attr("id"), strState:"1" },
function(data) {
$("#second_box").html(data);
});
} else {
$.ajax({
url: 'here/is/url/',
type: 'POST',
data: { strID:$(this).attr("id"), strState:"0" }
});
}
});
});
Here is the php code for ajax request :
$strID = $_POST['strID'];
$strState = $_POST['strState'];
if ($strState) { //if checkbox is clicked
//I fetch data about checkbox which is clicked
$infos = $this->info_model->get_info_from_checkbox_id($strID);
foreach ($infos as $info) {
echo "<label class='checkbox'>
<input type='checkbox'>".$info->information .
"</label>";
}
} else { // if it is unchecked
// I do not know how to delete
}
I want to add new selection on old ones.
you can use append() instead of html() which replaces the content:
$("#second_box").append(data);
how can I delete information in second box when I unchecked a checkbox.
you can use empty() which removes the contents of the selected element:
$("#second_box").empty();
Alternatively, you could do more on the client-side. See here for a demo.
<div id="first_box">
<label class='checkbox'>
<input data-url="url1" data-id="1" type="checkbox"> First checkbox
</label>
<label class='checkbox'>
<input data-url="url2" data-id="2" type="checkbox" > Second checkbox
</label>
<label class='checkbox'>
<input data-url="url3" data-id="3" type="checkbox"> Third checkbox
</label>
</div>
<div id="second_box"></div>​
JavaScript:
$(document).ready(function() {
$("#first_box input:checkbox").change(function(e) {
var $this = $(this);
if (already_loaded()) {
update_visibility();
} else {
load();
}
// END -- Functions
function already_loaded() {
return $this.data("loaded");
}
function is_loading() {
return $this.data("loading");
}
function load() {
if (!is_loading()) {
$this.data("loading", true);
var id = $this.data("id");
$.post("url", { // or use $this.data("url") if you want individual URLs
strId: id
}, function(data) {
$this.data("loaded", true);
$this.data("is_loading", false);
$("#second_box").append(data);
update_visibility();
});
}
}
function is_checked() {
return $this.is(":checked");
}
function update_visibility() {
var $info = $("#info-" + $this.data("id"));
if (is_checked()) {
$info.show();
}
else {
$info.hide();
}
}
});
$("#first_box input:checkbox").data("loaded", false);
$("#first_box input:checkbox").data("loading", false);
});​

Checkboxes in while loop won't give value

I'm trying to get all the values of my selected checkboxes into a textarea. The checkboxes and their values are taken from a DB. I don't know why, but the only thing my textarea is giving me is the word 'on' when something is selected.
these are the checkboxes:
<div class="controls" id="c_b">
<?php
echo "<ul>";
while($user = $allUsers->fetch_assoc())
{
echo "<li><input type='checkbox'> " . $user['username'] . " </input></li>";
}
echo "</ul>";
?>
<br />
</div>
and this is the function:
function updateTextArea() {
var allVals = [];
$('#c_b :checked').each(function() {
allVals.push($(this).val());
});
$('#t').val(allVals)
}
$(function() {
$('#c_b input').click(updateTextArea);
updateTextArea();
});
thanks in advance,
I hope somebody can see what I'm missing!
Jana
If you don't set a value on a checkbox, it will default to on if checked. You need to set the value to each username.
<input type="checkbox" value="bob" />
You may add the attribute value to the checkbox: http://jsfiddle.net/RNdFS/
You can try
$('#c_b input[type=checkbox]:checked').each(function() {
allVals.push($(this).val());
});

check all checkboxes

I have a table with checkboxes and I want to do the "check all" and "un-check all" checkboxes but I could not find the way to check all the checkboxes.
Here is my code:
<form>
<?php foreach ($this->entries as $entry): ?>
<tr class="table_head_seperator">
<td class="grid_info" width="32px" bgcolor="#f6f6f6"><input type="checkbox" class="chk_boxes1" name="to_delete[<?PHP echo $entry['id'] ?>]" /></td>
<td class="grid_info" width="160px" bgcolor="#eeeeee"><span class="country_name"><?php echo $entry['user_name'] ?></span></td>
<td class="grid_info" width="130px" bgcolor="#eeeeee"><span class="country_name"><?php echo $entry['date_created'] ?></span></td>
<td class="grid_info" width="100px" bgcolor="#f6f6f6"><span class="country_name"><?php echo $entry['user_type_name'] ?></span></td>
</tr>
<?PHP endforeach; ?>
<input type="checkbox" class="chk_boxes" label="check all" />check all
<input type="checkbox" class="unchk_boxes" /> un-check all
</form>
<script type="text/javascript">
$(document).ready(function() {
$('.chk_boxes').click(function(){
$('.chk_boxes1').attr('checked',checked)
})
});
</script>
$(function() {
$('.chk_boxes').click(function() {
$('.chk_boxes1').prop('checked', this.checked);
});
});
This only affects the check all box. But why would you want to use two checkboxes anyway? One to check all and one to uncheck all, but not mutually exclusive. That's got to be the recipe to confuse users :)
Try using true|false. Like so:
$('.chk_boxes1').attr('checked',true);
Additional comment:
In addition, it appears your un- and check all checkboxes are redundant.
<input type="checkbox" class="chk_boxes" label="check all" />check all
<input type="checkbox" class="unchk_boxes" /> un-check all
Rather than doing that, you can just have one checkbox that does both. Thus, your jQuery will look like:
$(document).ready(function() {
$('.chk_boxes').click(function(){
$('.chk_boxes1').attr('checked',$(this).attr('checked'));
})
});
With HTML:
<input type="checkbox" class="chk_boxes" label="check all" />check all
See the working solution here http://jsfiddle.net/HBGzy/
you don't need to use the "uncheck all" checkbox :)
$('.chk_boxes').click(function(){
var chk = $(this).attr('checked')?true:false;
$('.chk_boxes1').attr('checked',chk);
});
You forgot about quotes:
$('.chk_boxes1').attr('checked','checked')
fiddle: http://jsfiddle.net/8ZHFn/
Try this
$(".chk_boxes").click(function()
{
var checked_status = this.checked;
$(".chk_boxes1").each(function()
{
this.checked = checked_status;
});
});
So, if the class name of all the checkboxes you want to affect when checking or unchecking the checkbox with class chk_boxes,is chk_boxes1 this will get all the elements and set the checked property accordingly.
Check-uncheck all / select-unselect all checkboxes
Follow the following code , its really simple with the following code
inside your html form add the following line of code
<input type="checkbox" id='checkall' /> Select All
and than add the following script
<script type='text/javascript'>
$(document).ready(function(){
// Check or Uncheck All checkboxes
$("#checkall").change(function(){
var checked = $(this).is(':checked');
if(checked){
$(".checkbox").each(function(){
$(this).prop("checked",true);
});
}else{
$(".checkbox").each(function(){
$(this).prop("checked",false);
});
}
});
// Changing state of CheckAll checkbox
$(".checkbox").click(function(){
if($(".checkbox").length == $(".checkbox:checked").length) {
$("#checkall").prop("checked", true);
} else {
$("#checkall").removeAttr("checked");
}
});
});
</script>
keep in mind that .checkbox in script code is the class name from other checkboxes of html form
for example if you have input checkbox as following
<input class="checkbox" name="emailid[]" type="checkbox" value="<?php echo $email;?>" />
i think you should use the true or false for the checkbox attribute.
<script type="text/javascript" charset="utf-8">
$('document').ready( function() {
$('.chk_boxes').click( function() {
$(':checkbox').attr('checked',true);
});
});
</script>
An advice about check all/uncheck all..
As my think after selecting check all, if user clicks any of checkbox1s, the check all checkbox should be unchecked and also without clicking check all box, if user selects all checkbox1s one by one the checkbox should be selected. So i suggest you to try this when using check all/uncheck all..
$(document).ready(function() {
$('.chk_boxes').click(function(){
$('.chk_boxes1').attr('checked',$(this).attr('checked'));
})
$('.chk_boxes1').click(function(){
if($('.chk_boxes1').length == $('.chk_boxes1:checked').length)
$('.chk_boxes').attr('checked',checked)
else
$('.chk_boxes').attr('checked',false)
})
});

Categories