0

I want to create a command from the option and go to javascript then display it in the input box.

<select id="change">
    <option>Cat</option>
    <option>Dog</option>
    <option>Fish</option>
</select>

<script type="text/javascript">
show('Cat');
</script>

<input/>

when I choose one of the options, then the contents automatically in the javascript changed. and the reload function runs then displays the result in the input box.

3 Answers3

2

You can try adjusting to your best needs from this snippet

<select id="change">
    <option>Cat</option>
    <option>Dog</option>
    <option>Fish</option>
</select>

<script type="text/javascript">
  document.getElementById('change').addEventListener('change', function(e) {
    document.getElementsByTagName('input')[0].value = e.target.value;
  });
</script>

<input/>

If you want to call show with selected value you can have

document.getElementById('change').addEventListener('change', function(e) {
  var selected = e.target.value;
  show(selected);
});
1

Try this.

function a(select) {
var input = document.getElementById("input");
if (select.value !== "Select option") {
input.value = select.value;
}
else {
input.value=""
}
}
<select id="change" onchange="a(this)">
    <option>Select option</option>
    <option>Cat</option>
    <option>Dog</option>
    <option>Fish</option>
</select>

<input id="input" />
1

Hm not sure what your are trying to achieve but this works if your use a normal browser:

//Targets
select = document.getElementById("change");
print = document.getElementById("value");

//Function print option
printOption = function(loc, option) {
 loc.innerHTML = option;
}

//Function force value
optionPicker = function(option) {
 select.value = option;
  printOption(print, option);
};

//Change Listener
document.addEventListener("change", function(e){
 if(e.target.id === "change")
   printOption(print, e.target.value);
}, false);

//Commands
optionPicker("Dog");
<select id="change">
    <option>Cat</option>
    <option>Dog</option>
    <option>Fish</option>
</select>
<div id="value"></div>
Jens Ingels
  • 806
  • 1
  • 9
  • 12