Forum Moderators: open

Message Too Old, No Replies

Javascript on change <select> element

Javascript on change <select> element

         

madmatt69

12:34 am on Mar 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey everyone,

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 :)

daveVk

2:43 am on Mar 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure I understand requirement, for start try

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; }

madmatt69

3:33 am on Mar 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok - there was a minor syntax error in my code, but I fixed it now.

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?

madmatt69

3:44 am on Mar 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Got it :)

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>

daveVk

4:06 am on Mar 23, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A few points

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