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

1
  • you should pick one answer and accept it Commented Mar 7, 2014 at 8:33

7 Answers 7

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.

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

1 Comment

yup, and here's an official reference: w3schools.com/jsref/prop_option_text.asp
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/

Comments

0

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

Comments

0
function showResult()
{
   var value = this.options[this.selectedIndex].value;
   var content = this.options[this.selectedIndex].text;
}

Comments

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)

Comments

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.

Comments

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>

Comments

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.