Forum Moderators: open

Message Too Old, No Replies

Populating a hidden value via javascript

Populating a hidden value via javascript

         

danielm28

11:03 pm on Aug 26, 2004 (gmt 0)

10+ Year Member



Hello,

I need to populate a hidden value in an input form based on which department is selected. I need to retrieve the value of the selected department and then send it to another page. I use the value as a "TO" email address.

thx

here's the code.

Populating a hidden value via javascript

<script type="text/javascript">
<!--
function setEmail(toEmail)
{
var URL = ContactForm.Department.options[ContactForm.Department.selectedIndex].value;
}
// -->
</script>

</head>
<body>

<form name="ContactForm" action="SendForm.cfm" method="post">

<p>Name: <input type="text" name="Name" size="30" maxlength="100"></p>

<p>Email: <input type="text" name="Email" size="30" maxlength="100"></p>

<p>Select Department: <select name="Department" onchange="setEmail(this)">
<option value="daniel11m@4.com">General Information and Questions</option>
<option value="daniel@3.com">International Ordering</option>
<option value="danielm288@2.com">Marketing and Advertising</option>
<option value="danielm@1.com">Shipping, Tracking and Ordering Information</option>
</select>
</p>

<p>Body: <textArea name="Body" rows="10" cols="40"></textarea></p>

<p><input type="submit" name="Send" value="Send"></p>

<input type="hidden" name="toEmail" value="">

</form>

Bernard Marx

12:00 am on Aug 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




function setEmail(select)
{
var URL = select.options[select.selectedIndex].value;
alert('fluffy bunnies: '+URL)
select.form.toEmail.value = URL;
}

danielm28

2:46 pm on Aug 27, 2004 (gmt 0)

10+ Year Member



Thanks Bernard.

Actually, this is the function I used to get it to work:

function setEmail(toEmail)
{
var URL = ContactForm.Department.options[ContactForm.Department.selectedIndex].value;
ContactForm.toEmail.value = URL;
}

Bernard Marx

3:01 pm on Aug 27, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Fair enough, mate :)
But seeing as your event handler sends a reference to itself, you might as well use it.

onchange="setEmail(this)">


It's shorter, saves 3 milliseconds, and allows you to change the name of the form or the select without changing the details in the function - which could then be used for other, similarly functioning selects.