Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to equate value of hidden textbox with an array of radio buttons? 1

Status
Not open for further replies.
Feb 9, 2007
46
US
Hi,

I wish to grab the value of my hidden textbox and then go through an array of the radio buttons. Where there is a match, I would like to associate the checked property of that radio button to true. I am basically preserving the state of the radio button so that the users know which radio button they last clicked on when they click on the back button of the browser.

hCrystalRadio - name of the hidden textfield
openedCrystalItem - variable to hold the value of the textfield
crystalradio - name of the radio group that shows the names of the reports

Code:
//Remember the last report clicked on
	if(document.commission.hCrystalRadio.value != "")
	{
		openedCrystalItem = document.commission.hCrystalRadio.value
		alert('Here b4: '+ openedCrystalItem);
		for(i=0;i<document.commission.crystalradio.length;i++)
		{
			document.commission.crystalradio[openedCrystalItem[i]].checked = true;
		}
		
	}

I get an error at the above red line. Syntax is wrong. what is the right syntax to refer to the radio button?

Error I get is:

document.commission.crystalradio[...] is null or not an object.

Thanks.
 
Make these changes:
Code:
for(i=0;i<document.commission.crystalradio.length;i++) {
[!]   if (document.commission.crystalradio[i].value == openedCrystalItem) {[/!]
      document.commission.crystalradio[[!]i[/!]].checked = true;
[!]   }[/!]
}

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
Thanks Kaht. That worked like a treat.

Does the same logic works for the select boxes too? However the select box option value is different from the display value. For instance, option value is '12/31/2006' but the display value is 'December 2006'

I tried this, but the value which I selected last does not appear in the select box.

period - name of the select box
hPeriod - name of the hidden text field
Code:
//Remember the last date clicked on
		
	openedDateItem = document.commission.hPeriod.value
	alert(openedDateItem);
	for(i=0;i<document.commission.period.length;i++) {
	   if (document.commission.period[i].value == openedDateItem) {
	      document.commission.period[i].checked = true;
	   }
	}
 
Actually, you can just set the value of the <select> box directly. Like so:

Code:
document.formName.selectName.value = whatever;

If you prefer to do the loop like the radio buttons above, you can do it like so (but take note the first method is much faster, especially if you have a lot of <option> tags inside the <select>)

Code:
    for(i=0;i<document.formName.selectName.[!]options[/!].length;i++) {
       if (document.formName.selectName.[!]options[i][/!].value == whatever) {
          document.formName.selectName.[!]selectedIndex[/!] = i;
       }
    }

-kaht

Looking for a puppy? [small](Silky Terriers are hypoallergenic dogs that make great indoor pets due to their lack of shedding and small size)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top