Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

for each JavaScript Support in IE?

Tags:

javascript

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?

like image 504
user48094 Avatar asked Jan 05 '09 06:01

user48094


1 Answers

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); } 
like image 149
Matthew Crumley Avatar answered Sep 21 '22 13:09

Matthew Crumley