Forum Moderators: open
I've got a form select list of options. The idea is, that when a certain selection is made, an address will be written out. So I've been trying to use the following code:
<script type="text/javascript">
function getinfo()
{
var a=('Company Name<br />Street - City<br />country<br />phone')
}{
var mylist=document.getElementById("info")
if (document.getElementById("displayinfo").innerHTML=mylist.options[mylist.selectedIndex].title = 'a')
(
document.write(a);
}}
</script>
<select id="info" onchange="getinfo()">
<option title="a">Company A</option>
</select>
<div id="displayinfo">Choose a company</div>
So the idea behind this is if Company A is chosen from the list, the javascript grabs the var 'a' which contains the address info and prints it out.
I'm getting errors which I'm sure have to do with my syntax.
Can anyone figure out what I'm doing wrong? I'm a bit of a noob when it comes to javascript :)
REPLACE
if (document.getElementById("displayinfo").innerHTML=mylist.options[mylist.selectedIndex].title = 'a')
(
document.write(a);
}}
WITH
var val = mylist.options[mylist.selectedIndex].value;
if ( val == "a" ) { document.getElementById("displayinfo").innerHTML = val; }
Here's the current code though:
function getinfo()
{
var a=('Company Name')
var mylist=document.getElementById("info")
var val = mylist.options[mylist.selectedIndex].value;
if ( val == "a" ) { document.getElementById("displayinfo").innerHTML = val; }
}
</script>
<select id="info" onchange="getinfo()">
<option title="a">Company</option>
</select>
<div id="displayinfo">The title goes here</div>
I don't get any errors now, but it just doesn't work. I'm thinkign it has something to do with not knowing what the value of "a" is?
Final code looks like this:
<script type="text/javascript">
function getinfo()
{
var dna=('Company Name')
var mylist=document.getElementById("info")
var val = document.getElementById("displayinfo").innerHTML=mylist.options[mylist.selectedIndex].title
if ( val == "a" ) { document.getElementById("displayinfo").innerHTML = dna; }
}
</script>
<select id="info" onchange="getinfo()">
<option title="a">Company Name</option>
</select>
<div id="displayinfo">The title goes here</div>
1) If what your code to validate use "value" rather than "title" in these two lines
var val = document.getElementById("displayinfo").innerHTML=mylist.options[mylist.selectedIndex].value
<option value="a">Company Name</option>
2) To save specifing "company Name" twice
REPLACE
if ( val == "a" ) { document.getElementById("displayinfo").innerHTML = dna; }
WITH
if ( val == "a" ) { document.getElementById("displayinfo").innerHTML = mylist.options[mylist.selectedIndex].innerHTML; }
3) Not sure if first innerHTML assignment required, if so make it else condition