Como marcar todos los checkbox de una lista con JQuery
|Este es un código fácil y muy útil de implementar cuando tenemos una lista con varios items que deseamos marcar o desmarcar todos a la vez.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
<!DOCTYPE html> <html> <head> <title>Marcar todos los checkbox de una lista</title> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <!-- Libreria JQuery --> <script src="../jquery-1.11.2.min.js"></script> <script> $(document).ready(function () { //Detectar click en el checkbox superior de la lista $('#selectall').on('click', function () { //verificar el estado de ese checkbox si esta marcado o no var checked_status = this.checked; /* * asignarle ese estatus a cada uno de los checkbox * que tengan la clase "selectall" */ $(".selectall").each(function () { this.checked = checked_status; }); }); }); </script> </head> <body> <h1>Marcar todos los checkbox de una lista</h1> <!-- Tabla con items de ejemplo --> <table style="border: 1px solid black; padding: 5px;"> <thead> <tr> <th style="width: 15px;"><input id="selectall" type="checkbox"></th> <th>Marcar todos</th> </tr> </thead> <tbody> <tr> <td><input value="1" class="selectall" type="checkbox"></td> <td>Item 1</td> </tr> <tr> <td><input value="2" class="selectall" type="checkbox"></td> <td>Item 2</td> </tr> <tr> <td><input value="3" class="selectall" type="checkbox"></td> <td>Item 3</td> </tr> <tr> <td><input value="4" class="selectall" type="checkbox"></td> <td>Item 4</td> </tr> </tbody> </table> </body> </html> |
Demo.