Select/Unselect script does not work on firefox

Dear Experts hi,
  I am using the following script to select/unselect all select boxes on a form named reg. The select boxes are named c1 to c10
 The form declaration is as follows:

 <form id="reg" name="reg" method="post" action="something.php">
<a href="javascript:checkAll()">Select/Unselect All</a>    
<input name="c1" type="checkbox" id="something" value="1" />
.
.
.
<input name="c10" type="checkbox" id="something" value="10" />

 </form>

I have the firebug installed on my firefox and it gives me the error in question...
The funny thing is that I have set up another html page to use this script and it works great on firefox but not on the template I am using. I dont know if there is someother javascrpt code that I allready have set up on this template that conflicts and causes the error
reg is not defined var chkObj = eval('reg.' + chkArr[count]);

...either way any help will be appreciated. And of course if you need more info please ask. Thanks a lot...
function checkAll(){
	 		
            var chkArr = ["c1","c2","c3","c4","c5","c6","c7","c8","c9","c10"];
 
            var frmObj = document.getElementById('reg');
 
 
            for(var count=0;count < chkArr.length;count++){
                  var chkObj = eval('reg.' + chkArr[count]);
                  if(typeof(chkObj) != "undefined"){
                        chkObj.checked = !chkObj.checked;
                  }
            }
      }

Open in new window

atsalisAsked:
Who is Participating?
 
hieloConnect With a Mentor Commented:
The problem is here:
 var chkObj = eval('reg.' + chkArr[count]);

It doesn't know that reg is a child of document.forms. Fix:
var chkObj = eval('document.forms.reg.' + chkArr[count]);
0
 
Ashish PatelCommented:
Try this


function checkAll(){
	 		
            var chkArr = ["c1","c2","c3","c4","c5","c6","c7","c8","c9","c10"];
            //var frmObj = document.getElementById('reg');
 
            for(var count=0;count < chkArr.length;count++){
                  var chkObj = document.getElementsByName('reg.' + chkArr[count])[0];
                  if(chkObj && chkObj.type == "checkbox"){
                        chkObj.checked = !chkObj.checked;
                  }
            }
      }

Open in new window

0
 
atsalisAuthor Commented:
Thanks a lot it worked...however can I ask you why it used to work they way I did it before on another page?...? Do you think there may be some other Javascript tht I have that conflicts with that one?
0
 
hieloCommented:
>>Do you think there may be some other Javascript tht I have that conflicts with that one?
No.

>>why it used to work they way I did it before on another page
Can't tell for sure. One possibility is that "reg" on another page may have been a shortcut for
document.forms.reg
Ex:
var reg = document.forms.reg

then when evaled as you did, then it should work.
0
 
atsalisAuthor Commented:
Ok I guess that sets it...thanks a lot mate....
0
All Courses

From novice to tech pro — start learning today.