Re: OT: TECH: JavaScript help
From: | Philip Newton <philip.newton@...> |
Date: | Monday, December 22, 2008, 15:00 |
On Mon, Dec 22, 2008 at 15:35, Benct Philip Jonsson <bpj@...> wrote:
> Ideally the strings should be stored
> as values of a hash where the keys are the values of the
> options, but IIANM JavaScript knows nothing of hashes.
I believe you AM; IIRC the syntax for accessing an element of one is
variable['key'].
So perhaps something like this (lightly tested):
<script type="text/javascript">
mapping = {
'Marx' : 'Chico, Harpo, Groucho',
'Duck': 'Huey, Dewey, Louie',
'Simpson': 'Bart, Lisa, Maggie'
};
</script>
<input type="text" id="siblings" value="Chico, Harpo, Groucho">
<select name="families"
onchange="document.getElementById('siblings').value =
mapping[this.options[this.selectedIndex].text]">
<option value="Marx" selected= "selected">Marx</option>
<option value="Duck">Duck</option>
<option value="Simpson">Simpson</option>
</select>
And if you don't mind being sensitive to the order, you could use an
array instead of the hash and use this.selectedIndex directly rather
than having to indirect through this.options[this.selectedIndex].text
.
Cheers,
--
Philip Newton <philip.newton@...>