1

I don't know much about javascript and unfortunately don't have time to learn before this project is due (wish I did!). I assume it is possible to pass the value of a drop-down selection into a hidden text input field on a form before the form is submitted. Could anyone help me figure out how to do that with javascript? Thank you! Here are my drop-down and text box details:

<div class="formEntryArea">
<div class="formEntryLabel">
    <span class="formLabel"><label for=" langdropdown">Would you like to receive library notices in English or Spanish? ><span class="formRequired">*</span></label></span>
</div>
    <div class="formMultiSelect" id=”langdropdown”>
<select name=" langdropdown ">
<option value="0" selected="selected">Choose language</option>
<option value="eng">English</option>
<option value="spa">Spanish</option>
<input type="text" id="ddepartment" name="ddepartment"  value=””>
</select>
</div>

2

5 Answers 5

2

This is simply. First of all, you have to bind a change event handler for your select. Then, you have to set input text with value selected from dropdown.

var select=document.getElementsByTagName('select')[0];
var input=document.getElementById('ddepartment');
select.onchange=function(){
  input.value=select.options[select.selectedIndex].text;
}
<div class="formEntryArea">
<div class="formEntryLabel">
    <span class="formLabel"><label for=" langdropdown">Would you like to receive library notices in English or Spanish? ><span class="formRequired">*</span></label></span>
</div>
<div class="formMultiSelect" id=”langdropdown”>
    <select name=" langdropdown ">
       <option value="0" selected="selected">Choose language</option>
       <option value="eng">English</option>
       <option value="spa">Spanish</option>
    </select>
    <input type="text" id="ddepartment" name="ddepartment">
</div>

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

1 Comment

Thank you very much Alexandru-Ionut Mihai! I will give all of these options a try tomorrow.
1

You can use this code:

var myselect = document.getElementById("MySelect");
myselect.onchange = function(){
    alert(myselect.options[myselect.selectedIndex].value);
    document.getElementById("ddepartment").value = myselect.options[myselect.selectedIndex].value;
};

Result: https://jsfiddle.net/fh5myefw/

1 Comment

Thank you CMedina!!
0

Mind to close the tags, it's better practice.

var select = document.getElementById('selectElem');
var outputElem = document.getElementById('ddepartment');

select.addEventListener('change',function(){
  var newValue = !this.selectedIndex ? "":this.options[this.selectedIndex].text;
  outputElem.value = newValue;
});
    
<select name="langdropdown" id="selectElem" required>
  <option value="" selected="selected">Choose language</option>
  <option value="eng">English</option>
  <option value="spa">Spanish</option>
</select>  
<input type="text" id="ddepartment" name="ddepartment" value="">

Comments

0

this is javascript function

function func(selectObject) { document.getElementById('ddepartment').value = selectObject.value; }

add onchange event to select element like this

<select name="langdropdown" onchange="func(this)">

2 Comments

They are being lazy and asking us to do their homework. Not to mention three other people have already answered. Why would you post an answer?
Thank you very much, BARNI!! I have tried many different iterations of what you posted and just don't know enough to get it working. I will try some more.
0

Here use this:

var sel = document.getElementById('lang');

sel.onchange = function() {
  var val = this.options[this.selectedIndex].value;
  var che = document.getElementById('cache').value;
  che = val;
  console.log(che);
}

SNIPPET

var sel = document.getElementById('lang');

sel.onchange = function() {
  var val = this.options[this.selectedIndex].value;
  var che = document.getElementById('cache').value;
  che = val;
  console.log(che);
}
<select id='lang' name="lang">
  <option value="" selected>Choose language</option>
  <option value="eng">English</option>
  <option value="spa">Spanish</option>
  <option value="jpn">Japanese</option>
  <option value="zho">Chinese</option>
  <option value="fin">Finnish</option>
  <option value="nav">Navajo</option>
</select>

<input type="hidden" id="cache" name="cache" value=””>

1 Comment

Thank you very much zer00ne!! I will give all of these options a try tomorrow.

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.