views:

59

answers:

2

I'm working on a .NET-powered questionnaire which contains several repeater controls, each row of which contains three radiobuttons. I'm trying to write a simple Javascript function to check all the controls on the page which contain the word "chkGreen" in the control name/id.

The page looks something like this:

Repeater 1
Description 1           ( ) Green ( ) Yellow ( ) Red
Description 2           ( ) Green ( ) Yellow ( ) Red
Description 3           ( ) Green ( ) Yellow ( ) Red

Repeater 2
Description 1           ( ) Green ( ) Yellow ( ) Red
Description 2           ( ) Green ( ) Yellow ( ) Red
Description 3           ( ) Green ( ) Yellow ( ) Red

Here's the function so far:

   for (i = 0; i < document.Form1.elements.length; i++) {
      var _control = document.Form1.elements[i].id
      if (_control.indexOf("chkGreen") > 0) {
         eval(_control.checked = true);
      }
   }

This function does not work. When I add a document.write or alert() to the statement, it properly fires, so the logic is apparently working, it's just the actual radiobutton check code is not working.

Any thoughts?

A: 

You don't need to eval the _control.checked line.

for (i = 0; i < document.Form1.elements.length; i++) {
  var _control = document.Form1.elements[i]; // EDIT: you want the element, not the id of the element
  if (_control.indexOf("chkGreen") > 0) {
     _control.checked = true;
  }
}
Mark Brackett
A: 

Thanks for the quick response. I tried the function without using eval() and it didn't work. I have since gotten the function to work by using document.getElementById:

for (i = 0; i < document.form1.elements.length; i++) {
  var _control = document.form1.elements[i].id
  if (_control.indexOf("chkGreen") > 0) {
    var thecontrol = document.getElementById(_control);
    thecontrol.checked = true;
  }
}
Chad
argh...I missed the .id you were putting at the end of the var _control line. Edited.
Mark Brackett