PDA

View Full Version : automatically populate a hidden field based on a drop-down selection



fizzyfozzy
02-28-2012, 02:13 PM
I have a form with a drop-down for Title (Mr, Mrs, Miss, Ms)
I'd like the value of a hidden field for Gender to be automatically updated according to the title selected.

So I need:

If Title.value = Mr
Then
Gender.value = Male
Else
Gender.value = Female

Except I can't get the syntax right, could someone give me a hand? :confused:

vwphillips
02-28-2012, 03:40 PM
<!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" xml:lang="en" lang="en">

<head>
<title></title>
<script type="text/javascript">

function Gender(sel,nme){
var txt=sel.options[sel.selectedIndex].text;
sel.form[nme].value=txt.indexOf('M')==0?txt.indexOf('s')>0?'Female':'Male':'';
}

</script></head>

<body>
<form>
<select onchange="Gender(this,'gender');" >
<option >Title</option>
<option >Mr</option>
<option >Mrs</option>
<option >Ms</option>
<option >Miss</option>
</select>
<input name="gender" />
</form>
</body>

</html>

fizzyfozzy
02-28-2012, 03:55 PM
Thanks vwphillips!

Not how I thought at all but that did the job. Fantastic, thank you :)