Jump to content

[SOLVED] using a value from a select box in a javascript variable


simon551

Recommended Posts

trying to take the value from a select box and use it in a js variable. any suggestions?

 

      <td nowrap align="right">Default Currency </td>
      <td><select name="curr" id="curr">
        <option value="USD">USD</option>
        <?php
do {  
?>
        <option value="<?php echo $row_rsCurr['CurrCode']?>"><?php echo $row_rsCurr['CurrCode']?></option>
        <?php
} while ($row_rsCurr = mysql_fetch_assoc($rsCurr));
  $rows = mysql_num_rows($rsCurr);
  if($rows > 0) {
      mysql_data_seek($rsCurr, 0);
  $row_rsCurr = mysql_fetch_assoc($rsCurr);
  }
?>
      </select></td>
    </tr>
    <tr valign="baseline">
      <td nowrap align="right">Amount Exchanged (USD) </td>
      <td><input type="text" name="textfield" /></td>
     </tr>
<script type="text/javascript">
var currname=this.form.curr.value
</script>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Amount Recieved (<script type="text/javascript"> document.write(currname)</script>) </td>

Thanks for the reply. Trying this:

	<script type="text/javascript">
var currname=document.getElementById('curr').value;
</script>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Amount Recieved (<script type="text/javascript"> document.write("currname")</script>
      ) </td>

 

also trying this. Trying to call the function:

<select name="curr" id="curr" onchange=writeCurrName;>

 

The function:

	<script type="text/javascript">
function writeCurrName(){
var currname=document.getElementById('curr').value;
document.getElementById("amtrecmsg").innerHTML = "Currency Recieved (" +currname + ")";
}
	</script>

 

And just so I'm not leaving anything out, the field to change:

<td nowrap="nowrap" align="right" id="amtrecmsg">

Just in case anyone is trying to do this, I figured it out. I put the script in a separate file and called it within the script rather than trying to call it from the onchange in the form, and it works now. Not exactly sure why. btw: does anyone know of a more active javascript forum?

window.onload=writeCurrName;

//the following function does the work: creates the string to be placed into the lable field and sets up the field
function getname(currname){
var currname=document.getElementById('curr').value;
			document.getElementById("amtrecmsg").innerHTML = "Currency Recieved (" + currname + ")";

}
//this is the trigger
function writeCurrName(){
document.getElementById("curr").onchange=getname;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.