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!

Referring to the name of a form element with a variable

Status
Not open for further replies.

dwhalen

Programmer
Feb 22, 2002
105
CA

I am trying to create a function that will access the value of different option boxes. When I call the function I want to pass in the name of the option box and then access its properties. Can anyone help? Here is something like what I am doing:


function viewReport(name){

if(eval("document.rptselt.name.options[document.rptselt.name.selectedIndex].value==''")){
alert('Please select one option to view report');
}

else{
eval('document.rptselt.rpt.rptparm.value=document.rptselt.name.options[document.rptselt.name.selectedIndex].value');


window.open('/work/report/report.pl?rptparm='+document.rptselt.rptparm.value,'Report','scrollbars=yes,resizable=yes');

}


}//end function

Thanks
 
Code:
function viewReport(name){

if(eval("document.rptselt."+name+".options[document.rptselt."+name+".selectedIndex].value==''")){
alert('Please select one option to view report');
}

else{
eval('document.rptselt.rpt.rptparm.value=document.rptselt."+name+".options[document.rptselt."+name+".selectedIndex].value');


 window.open('/work/report/report.pl?rptparm='+document.rptselt.rptparm.value,'Report','scrollbars=yes,resizable=yes');

}


}//end function
like that? hope it helps Suceess, thats the way you spell success!
Got a question? Ask IMOZRMY!
 
hi there

dwhalen, you can access your form elements thru form array by their names: document.forms[formname][elementname]
or using dots-syntax (like you did there) with or without eval function

you're almost at the point, check this:
Code:
function viewReport(name){
  d=document; df=d.forms;
  _fn="rptselt";
  _f=df[_fn];
  _sel=_f[name];
  _so=_sel.options;
  _si=_sel.selectedIndex;
  _chosen=_so[_si];
if (_chosen.value=="") alert("something")
else _f.rpt.rptparm.value=_chosen.value;
}
i wrote almost the same what you did, but i think this code is much more readable (but who knows.. i like it, but may be you (& everybody else) don't)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top