tags:

views:

576

answers:

3

I know a good amount of PHP, but know very little of Javascript.

Basically, what I want is:

If a user selects item x from an HTML form select box, then the descriptive text for item x will auto populate an HTML form input text box below it.

Does anyone have a working sample that I can use or edit to be able to do what I need?

A: 

Based on the example at http://w3schools.com/js/tryit.asp?filename=tryjs_putmore

<html>
<head>
<script type="text/javascript">
function moveNumbers()
{
var no=document.getElementById("no");
var option=no.options[no.selectedIndex].text;
document.getElementById("result").value=option;
}
</script>
</head>

<body>
<form>
Select numbers:<br />
<select id="no" onchange="moveNumbers()">
    <option>0</option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    <option>6</option>
    <option>7</option>
    <option>8</option>
    <option>9</option>
</select>
<input type="text" id="result" size="20">
</form>
</body>

See http://w3schools.com/htmldom/dom%5Fobj%5Fselect.asp and http://w3schools.com/htmldom/dom%5Fevents.asp for more info.

jrummell
A: 

It would help if the input box has an id:

<input type="text" id="someinput" value="" />

Then you need an event listener on the select box.

<select id="someselect">...</select>

In javascript:

document.getElementById('someselect').onchanged = function() {
var selem = document.getElementById('someselect'); 
document.getElementById('someinput').value = selem.options[selem.selectedIndex].value;
}

This is a very rough idea.

James Black
A: 

Here is a working copy in plain JavaScript using no libraries:

<script type="text/javascript">
    // Pre populated array of data
    var myData = new Array();
    myData[1] = 'Some text';
    myData[2] = 'Some other text';
</script>     
<form id="example" name="example">
        <select id="selector" name="selector">
         <option value="" selected></option>
         <option value=1>One</option>
         <option value=2>Two</option>
        </select>
        <br />
        <input type="text" value="" id="populateme" name="populateme" />
    </form>

    <script type="text/javascript">
        document.example.selector.onchange = updateText;

        function updateText() {
          var obj_sel = document.example.selector;
          document.example.populateme.value = myData[obj_sel.value];
    }
    </script>
Rob Booth
Where does it pull the text from? The text that it needs to populate the textbox with is not in the select box. See my comment above.
Citizen
Sorry I submitted this before your added comment. If your form/page doesn't already have the text you want to update the text box with you will need to do some Ajax calls to get that data. Maybe you should post your form to help clarify this issue, cause with your question and comment I can't figure out exactly what you are looking for.
Rob Booth
I can put the text on the page ahead of time. I'm assuming I would put them in array.
Citizen
Ok, I've updated my code to show it taking data from a pre-populated javascript array.
Rob Booth
Thank you very much!
Citizen