I have this code:
<script> function toggle(source) { checkboxes = document.getElementsByName('DG1'); for each(var checkbox in checkboxes) checkbox.checked = source.checked; checkboxes = document.getElementsByName('DG2'); for each(var checkbox in checkboxes) checkbox.checked = source.checked; checkboxes = document.getElementsByName('DG3'); for each(var checkbox in checkboxes) checkbox.checked = source.checked; checkboxes = document.getElementsByName('DG4'); for each(var checkbox in checkboxes) checkbox.checked = source.checked; checkboxes = document.getElementsByName('DG5'); for each(var checkbox in checkboxes) checkbox.checked = source.checked; } </script> <input type="checkbox" onClick="toggle(this)" />Select All<br/> <form method=POST action="DGUsageServlet"> <input type="checkbox" name="DG1">DG1</input> <input type="checkbox" name="DG2">DG2</input> <input type="checkbox" name="DG3">DG3</input> <input type="checkbox" name="DG4">DG4</input> <input type="checkbox" name="DG5">DG5</input> </form>
How can I make the above script to work in IE?
Note that Firefox no longer supports for each loops, so this applies to every browser, not just Internet Explorer. See this MDN article for alternatives.
Internet Explorer doesn't support "for each" loops (along with other modern browsers, which have dropped support for them). You will need to change the code to use regular for loops:
function toggle(source) { var checkboxes = document.getElementsByName('DG1'); for (var i = 0; i < checkboxes.length; i++) { checkboxes[i].checked = source.checked; } ... }
Or, you could use a library like jQuery and do it like this:
function toggle(source) { $("input[name^=DG]").attr("checked", source.checked); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With