Dynamic drop down select and field population?

Hi,

I'd like to be able to have a drop down select that will dynamically populate text fields w/ data when a user selects from the drop down list.

So far, I found an example that works and dynamically changes the text in just a div header when i select from the drop down

Code is as follows:
===============
<script type="text/javascript">
function getinfo()
{
var dna=('Company Name test')

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>
<option title="b">Company Name1</option>
<option title="c">Company Name2</option>

</select>

<div id="displayinfo">The title goes here</div>
==============

However, when I try to modify this so that it changes the text in a text box field instead it doesn't seem to work.

Code as follows:
==============
<script type="text/javascript">
function getinfo()
{
var dna=('Company Name test')

var mylist=document.getElementById("info")
var val = document.getElementById("bob").innerHTML=mylist.options[mylist.selectedIndex].title

if ( val == "a" ) { document.getElementById("bob").innerHTML = dna; }
}
</script>

<select id="info" onchange="getinfo()">
<option title="a">Company Name</option>
<option title="b">Company Name1</option>
<option title="c">Company Name2</option>

</select>

<input type="text" id="bob">
<div id="displayinfo">The title goes here</div>
=============
Anyone have any idea why this doesn't work?
DImhoff1Asked:
Who is Participating?
 
bugs021997Connect With a Mentor Commented:
@SirCrofty, I apologies, did not refresh before posting...


But two lines in @DImhoff1

Change

var val = document.getElementById("bob").innerHTML=mylist.options[mylist.selectedIndex].title

To

var val = document.getElementById("bob").value=mylist.options[mylist.selectedIndex].title



AND Change

if ( val == "a" ) { document.getElementById("bob").innerHTML = dna; }

TO

if ( val == "a" ) { document.getElementById("bob").value= dna; }
0
 
SirCroftyConnect With a Mentor Commented:
I thiink you just need to change this line

document.getElementById("bob").innerHTML = dna;

to

document.getElementById("bob").value = dna;
0
 
SirCroftyConnect With a Mentor Commented:
I thiink you just need to change this line

document.getElementById("bob").innerHTML = dna;

to

document.getElementById("bob").value = dna;
0
 
bugs021997Connect With a Mentor Commented:
The problem in your code is...

var val = document.getElementById("bob").innerHTML=mylist.options[mylist.selectedIndex].title

You are trying to specify innerHTML to a TEXTBOX, which is wrong....

Change that line to as below...

var val = document.getElementById("bob").value=mylist.options[mylist.selectedIndex].title
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.