How can I create a checkbox that selects all others from my page? I'm using a while loop in php to list the records, and each one has its checkbox next to it. I would like to have a checkbox on the page that, when selected, would mark all the others, in order to delete, edit .. similar situation to Hotmail, Gmail in the inbox.
Loop in PHP:
<form action="deleta.php" method="post">
<ul class="lista-posts">
<li class="seleciona"><input type="checkbox" id="checkbox" name="deletar[]" value="<?php echo $row['id']; ?> " /></li>
<li class="titulo"><?php echo '.$row['titulo'].'; ?></li>
</ul>
</form>
Resolved as follows:
HTML
<input type=checkbox name=1>
<input type=checkbox name=2>
<input type=checkbox name=3>
<input type=checkbox name=all>
JAVASCRIPT
document.querySelector("input[name=all]").onclick = function(e) {
var marcar = e.target.checked;
var lista = document.querySelectorAll("input");
for ( var i = 0 ; i < lista.length ; i++ )
lista[i].checked = marcar;
};