0

I have a drop-down list. After a user selects a language, the selected language is sent to the server. I want to show the selected values on the selected option button.

For example, a user selects a "de", I wish "de'" is displayed on the form, instead of "en".

How can I do this? Thanks

<form name="languages" method="post"> 
    <select name="langSelect" onchange=""> 
       <option>en (default)</option> 
       {% for ele in comments.languages.all %} 
           <option>{{ele.lang}}</option> 
       {% endfor %} 
    </select> 
    <div><a  onclick="submitform()">Submit</a></div> 
</form> 
susanna
  • 1,395
  • 3
  • 20
  • 32
  • where and when do you want to show the values? give your option tags a value attribute **** – cbayram Oct 19 '12 at 18:02
  • you can execute a function similar to this one http://stackoverflow.com/questions/610336/javascript-retrieving-the-text-of-the-selected-option-in-select-element in your submitForm after you've submitted the form. – cbayram Oct 19 '12 at 18:05

3 Answers3

0

Value of select box will contain selected option value.

var selectedValue = document.getElementById("langSelect").value
Anoop
  • 23,044
  • 10
  • 62
  • 76
0

First off, why is your button not a Submit button? If it submits the form you should use <input type="submit">Submit</input>

If you use jQuery, then you can say:

var selectedVal = $("#langSelect").val();
$("#languages submit").val(selectedVal);

I would put this in one statement but did it like this for clarity.

One other thing, you should use the id attribute as well/instead of just Name.

Roddy of the Frozen Peas
  • 14,380
  • 9
  • 49
  • 99
John Mc
  • 2,862
  • 1
  • 22
  • 37
0

Better use jQuery to achieve that, IE may have some issues (as always): http://www.impressivewebs.com/avoiding-problems-with-javascript-getelementbyid-method-in-internet-explorer-7/

In jQuery just type

$('[id=langSelect]').val(); 

for getting a value and

val('myValue')

for setting it. So you may set your name by

$('[id=selectedOptionButton]').val($('[id=langSelect]').val());
UNeverNo
  • 549
  • 3
  • 8
  • 29