2
I looked for the solution here in the forum, however they did not work.
I want to select all the checkbox
by clicking only 1 checkbox
(selects all).
Follows my code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link rel="stylesheet" href="css/app.min.css">
<meta name="description" content="Violate Responsive Admin Template">
<meta name="keywords" content="Super Admin, Admin, Template, Bootstrap">
<script>
$(document).ready(function(){
//Para fins de depuração:
console.log("Entrei aqui.");
$("#checkMaster").click(function(){
//Para fins de depuração:
console.log("Checkbox mestre foi clicado!");
$('input:checkbox').not(this).prop('checked', this.checked);
});
console.log("saiu aqui.");
});
</script>
<!-- <meta http-equiv="refresh" content="5" /> -->
</head>
Warning
<div class="card">
<div class="card-body">
<div class="table-responsive">
<table id="data-table" class="table">
<thead>
<tr>
<th>Nome</th>
<th>Telefone</th>
<th>Numero de tentativas</th>
<th>Ultima Tentativa</th>
<th>Zerar Tentativas?</th>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td><p><input type="checkbox" id="checkMaster" name="checkTodos"> Selecionar Todos</p></td>
</tr>
<?php
$query = sprintf("select * from ivr_contatos, ivr_campanha where ivr_contatos.campanha = ivr_campanha.id and ivr_contatos.status = 0 and tentativas >= 5" );
$result = Populator::ConsultaDB($query);
while ($resultado = pg_fetch_array($result) ) {
$nome = $resultado['nome'];
$telefone = $resultado['telefone'];
$tentativa = $resultado['tentativas'];
$lastAttempt = $resultado['atualizado'];
$codigo = $resultado['codigo'];
echo '
<tr>
<td>'.$nome.'</td>
<td>'.$telefone.'</td>
<td>'.$tentativa.'</td>
<td>'.substr($lastAttempt,0,19).'</td>
<td><input type="checkbox" value='.$codigo.' name="check[]"/></td>
</tr>
';
}
?>
</tbody>
</table>
</div>
</div>
</div>
<input type="submit" value="Resubmit" class="btn btn-sm m-r-5" />
</form>
Any idea how to fix this??
Edit: Dear, good afternoon, I realized the recommendations of this post, however I’m still with the same problem. Is there any other way I can go to achieve this goal?
$('input:checkbox').not(this).prop('checked', this.checked);
init(16) [input#ckball, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, prevObject: init(16), context: document]
The code seems ok to me! What is not working?
– Sam
When I click on the checkbox select all, the others are not marked as checked
– Willian Lima
Your code is working, see here https://jsfiddle.net/Lwamk47c/
– Sam
Try to put the code inside
$(document).ready(function(){ código aqui });
– Sam
I tried, but tbm didn’t roll, I’m excited about it man, jsfiddle works, web page does not...
– Willian Lima
See if it shows an error in the console by clicking the boss checkbox
– Sam
On the console this showing error on this line $('input:checkbox').(this). prop('checked', this.checked); shows this error, but I can’t find it. Uncaught Syntaxerror: Unexpected token (
– Willian Lima
That one
.(this)
is wrong... the correct is$('input:checkbox').prop('checked', this.checked);
– Sam
I made the change, the error in the console is gone, but it does not select the other checkbox
– Willian Lima
Are you sure there is only 1 id with "checkTodos"?
– Sam
go to the console and run
$("#checkTodos").length
... if return more than 1 there is bad– Sam
Executed, he returned only 1
– Willian Lima
So the only output is vc show the whole code, only the part that involves javascript and the table.
– Sam
posted in response.
– Willian Lima
With the
$(document).ready(function(){
worked– Sam
$(document).ready(function(){
 $("#checkTodos").click(function(){
 $('input:checkbox').prop('checked', this.checked);
 });
 });
– Sam
I made the change, however it persists... Maybe, the bootstrap hides when the others are checked? or it would have no influence?
– Willian Lima
Caique, I performed the tests, and he returns the following information to console.log( $('input:checkbox').not(this).prop('checked') ); = false; console.log( $('input:checkbox').not(this) ); VM2617:1 init(16) [input#ckball, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, input#ckbCod.check, prevObject: init(16), context: Document]
– Willian Lima
@Caiqueromero then, the question of the ID is how this checkbox is generated whenever a new record is made in the bank.
– Willian Lima
You put fixed
<td><input type='checkbox' id='ckbCod'></td>
this is within a loop, so your html will contain several<input />
with evenid
understands?– Caique Romero
Yes.. but this would be a necessity of the application pq whenever there is a record in the database would be created a checkbox for it. Is there no way I can select all checkboxes even with the same ID? or create a dynamic ID?
– Willian Lima
@Caiqueromero he comes to rotate, I will put in the question for you see the answer
– Willian Lima
by the console it shows that this as checked but visually not shown
– Willian Lima
Let’s go continue this discussion in chat.
– Caique Romero