# Javascript variable substitution for objects on page



## Lori713 (Sep 8, 2003)

I am trying to write Javascript that will pass a different object into a function depending on which checkbox has been checked in a particular cell. If I hard-code the object name (i.e., nc_rpt1), it works fine. But if I try to use a variable to pass the object, it will not work. If I replace the hard-coded "nc_rpt1" with "thisField" on line 47, it does not work.

I would like to be able to have a small function in Javascript that will recognize which object is being clicked (nc_rpt1 or nc_rpt2 or nc_rpt3) without having to hard-code separate sections for each item.

Below is the code I have. I have pared down the xhtml code to the bare minimum but it should be enough to allow someone to help me figure out what I'm missing. I'd appreciate any help you can provide. Thanks!


```
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><title>Wolfpack Reporting System</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
</head>
<body>

<form name="form_prefs" method="post" action="ncw_prefs.pl">
<table class="bold" width="100%" border="0">
<tr><td class="section" colspan="6" style="text-align:left;">Check the reports and links you want to appear on your Main Menu</td></tr>

<tr><td style="text-align:center; text-decoration:underline;">Reports</td>
    <td style="text-align:center; text-decoration:underline;">Links</td><td>&nbsp;</td></tr>
<tr>
<td><input type="checkbox" name="nc_rpt1" id="nc_rpt1" value="nc_rpt1" 
           onclick="checkRpt('nc_rpt1');"
           style="cursor:pointer;"   />
    <label for="nc_rpt1">&nbsp;&nbsp;1 - Fiscal Year-to-Date Financial Status</label></td>
</tr>

<tr>
<td><input type="checkbox" name="nc_rpt2" id="nc_rpt2" value="nc_rpt2"
           onclick="checkRpt('nc_rpt2');"
           style="cursor:pointer;"  />
    <label for="nc_rpt2">&nbsp;&nbsp;2 - Project-to-Date Financial Status</label></td>
</tr>

<tr>
<td><input type="checkbox" name="nc_rpt3" id="nc_rpt3" value="nc_rpt3"
           onclick="checkRpt('nc_rpt3');"
           style="cursor:pointer;" checked="checked" />
    <label for="nc_rpt3">&nbsp;&nbsp;3 - Cash Balance</label></td>
</tr>
</table>
</form>

<script type="text/javascript"> <!--

function checkRpt(thisField)
{
checkSet = eval("document.form_prefs." +thisField);
alert("thisField " +thisField);
alert("in the function");

if      ( checkSet.checked )
        { alert("in if stmt");
          document.form_prefs.nc_rpt1.value = 'Y';
          alert("what is it 1" +document.form_prefs.nc_rpt1.value);
        }
else if ( !(checkSet.checked) ) 
        { alert("in else stmt");
          document.form_prefs.nc_rpt1.value = 'Ethel';
        }

alert("what is it " +document.form_prefs.nc_rpt1.value);
}
 // -->
</script><noscript>Your browser must have Javascript enabled in order to use these reports.</noscript>
</body></html>
```


----------



## s1dev (Jun 25, 2004)

Lori713, 

You've almost got it.

You are passing the _name_ of the checkbox object to your function, but in line 47 you are trying to use it as an object. No good.

You are correctly calculating the object variable of your checkbox (checkSet)

So when thisField is equal to "nc_rpt1", checkSet is equal to document.form_prefs.nc_rpt1.

line 46: checkSet.value = 'Y';
line 47: alert("what is it 1" + checkSet.value);

line 51: checkSet.value = 'Ethel';

line 54: alert("what is it " +checkSet.value);

hth


----------



## s1dev (Jun 25, 2004)

PS -

You can simplify it by passing an object, and then elimating the line where you assign checkSet:

onclick="checkRpt(this);"

and then:

function checkRpt(checkSet) {
.
.
.


----------

