0
<select onchange="showResult(this.value)">
    <option value="AAA">London</option>
    <option value="BBB">France</option>
    <option value="ccc">Berlin</option>
    <option value="DDD">Rome</option>
</select>

In above code, the value of each <option> has been passed as parameter in showResult().

My questions is how to pass the content of option element (i.e.'London','France', 'Berlin', 'Rome') as parameter in showResult().

Many Thanks

Manse
  • 37,765
  • 10
  • 83
  • 108
Acubi
  • 2,793
  • 11
  • 41
  • 54

7 Answers7

1

[yourselect].options[0].text returns 'London', [yourselect].options[1].text France, etc. So, in other words, for every option of an options-nodeList the property text contains its content.

KooiInc
  • 119,216
  • 31
  • 141
  • 177
1

write this code

<select onchange="showResult(this.options[this.selectedIndex])">
    <option value="AAA">London</option>
    <option value="BBB">France</option>
    <option value="ccc">Berlin</option>
    <option value="DDD">Rome</option>
</select>

so you will pass the whole selected option node to the showResult function and you will be able to access both the value and text

function showResult(opt) {
    alert(opt.value);     /* e.g. DDD */
    alert(opt.text);      /* e.g. Rome */
}

Example Fiddle : http://jsfiddle.net/xypGa/

Fabrizio Calderan
  • 120,726
  • 26
  • 164
  • 177
0

Try this.options[this.selectedIndex].text, been a while since I did this without jQuery.

Mikael Härsjö
  • 1,048
  • 6
  • 14
0
function showResult()
{
   var value = this.options[this.selectedIndex].value;
   var content = this.options[this.selectedIndex].text;
}
mihai
  • 37,072
  • 9
  • 60
  • 86
0

Is this question about your issue - Get selected value in dropdown list using JavaScript? ?

in general - document.FORM.FIELDMANE.options[document.FORM.FIELDMANE.selectedIndex].value

in your case it should be i suppose - onchange="showResult(this.options[this.selectedIndex].value)

Community
  • 1
  • 1
shershen
  • 9,875
  • 11
  • 39
  • 60
0

Instead of passing this.value (the value property of the select element), pass this (the select element), i.e. onchange="showResult(this)", and use code like

function showResult(selectElem) {
  var optionText = selectElem.options[selectElem.selectedIndex].text;
  alert(optionText); // replace by real code that uses the string
}

An added advantage is that now the function has access to the entire element, so that if you later need to use the value property, too, in the function, you can do that without modifying the function calls.

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
0
<select onchange="showResult(this.options[this.selectedIndex].text)">
    <option value="AAA">London</option>
    <option value="BBB">France</option>
    <option value="ccc">Berlin</option>
    <option value="DDD">Rome</option>
</select>
Hắc Huyền Minh
  • 1,025
  • 10
  • 13