1

I have a dropdown form that I have created - I need to have the dropdown either be auto-generated to a text box once selected or have it placed in the text box once a button has been pressed. For example "Get Hex"

Here is my dropdown:

<fieldset>
  <legend>Logos</legend>
  <p>
    <label>Choose Desired Logo</label>
    <select id = "Logo">
      <option value = "00">Holden</option>
      <option value = "01">HSV</option>
      <option value = "02">Chevrolet</option>
      <option value = "02">Chevrolet</option>
      <option value = "04">CSV</option>
      <option value = "05">Pontiac</option>
    </select>
    <input type="button" value="Show Hex" onclick="displaySelectedItem(val);" />
  </p>
  <p>
    <input type="text" id="Logo" />
  </p>
</fieldset>

And here is the last JS script i've tried. I have tried a few but I am surely missing something as nothing I command works.

<script language="JavaScript" type="text/javascript">
<!--
function displaySelectedItem(val)
{
  alert(val);
}
//-->
</script>

So I need for the text box to contain the hex code in the <option value = "##">

I'll be using this throughout 6 different forms, all needing the same thing.

1
  • I would put the onclick event on the select-tag Commented May 8, 2013 at 19:52

1 Answer 1

3

Change

<input type="button" value="Show Hex" onclick="displaySelectedItem(val);" />

to

<input type="button" value="Show Hex" onclick="displaySelectedItem(document.getElementById('Logo').value);" />

jsFiddle example

Sign up to request clarification or add additional context in comments.

13 Comments

jsfiddle.net/DP5nw/1 --- Here is the updated example with what he wanted, which is get the value of select and put into input but @Neolisk edited the question and removed that part of what he asked.
That worked, for the most part. Haha. Why did it do a pop up and not generate to the text field below, as seen in the example?
I justed used the alert because you did. You can remove it and change it to document.getElementById('Logo2').value = val; (note that IDs muct be unique and in your example you re-used "Logo"). See jsfiddle.net/j08691/DP5nw/2
757goat.org/gto_cluster/Worksheettestjs.php - In the jsfiddle it works perfectly, but in IE and Chrome it results in a popup box. Is this due to something i've commanded?? Edit: This is a link to the document i'm working on. It's a plain jane test site
Yes it should, although without seeing all your code I can't say with 100% certainty.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.