2
My problem is that I have a list, in this list all items have a button and a checkbox, what should happen is, I click the button of this item and it select only the checkbox of this item. But what I managed to do is just click on any button and it select all the checkbox.
My view
<?php foreach ($produtos as $produto): ?>
<tr>
<td><?= $this->Number->format($produto->id) ?></td>
<td><?= h($produto->name) ?></td>
<td><?= h($produto->cor) ?></td>
<td><?= h($produto->tecido) ?></td>
<td><?= $this->Number->format($produto->estoque) ?></td>
<td><?= $this->Number->format($produto->preco) ?></td>
<td>
<button type="button" class="btn-success btn addcart" onclick='marcardesmarcar();'>+ Adicionar</button>
<input type="checkbox" style="display:none;" name="id[]" class="checkbox checkTodos" value="<?php echo $produto['id'];?>">
</td>
</tr>
<?php endforeach; ?>
Javascript:
var clicked = false;
$(".addcart").on("click", function() {
$(".checkTodos").prop("checked", !clicked);
clicked = !clicked;
});
Thanks for the help, ah these parts that Voce removed I’m using in other parts of the code, however I forgot to remove them when posting, forgive me. Only problem is that I am using the foreach to list me all products, because to do this by parts as Voce did is impracticable for me if I have a large number of products... Is there no other way to solve my problem? How to generate id for each checkbox automatically?
– LMT
Sorry if I’m talking nonsense, I’m new to js
– LMT
As you are drawn the table should not affect the operation of javascript, I put so to have more than one checkbox/ product and be able to see the code working, imagine that the html presented here is the result of your
foreach
in the php– Icaro Martins
Oh understood, thank you for the reply and sorry for my confusion in the previous comment, thank you very much!
– LMT