1

i would like to ask you help.

If i have an tag with a specific (value="yyy") and some text, how can i retrieve this text or check if it is equal to a specific string if the option is selected, through jQuery?

<option name="xxx" value ="yyy">text</option>

I know that the .val() function retrieves the value="yyy" and not the text. I need to get (and check) the text string.

Thanks

Daniele
  • 25
  • 1
  • 4
  • possible duplicate of [jQuery get specific option tag text](http://stackoverflow.com/questions/196684/jquery-get-specific-option-tag-text) – dsgriffin Jun 26 '14 at 23:16
  • Why are you giving the `option` a `name` tag? That's normally in the `select`. – rybo111 Jun 27 '14 at 08:42

2 Answers2

1

please see fiddle

http://fiddle.jshell.net/RrD74/

html:

   <select id="mySelect">
        <option name="xxx" value="1">text opt 1</option>
        <option name="xxx" value="2">text opt 2</option>
        <option name="xxx" value="3">text opt 3</option>
    </select>

js:

 $(function () {
        $("#mySelect").change(function () {
            alert($("#mySelect option:selected").text());
        });


    });
sylwester
  • 16,498
  • 1
  • 25
  • 33
  • Thanks, but if I have more then one option? Your example retrives all text inside the options tag... I want to get the text inside only the specific chosen one – Daniele Jun 27 '14 at 08:00
1

The code I believe you are looking for is:

var my_val = "yyy";
var txt = $('option[value='+my_val+']').text();
if(txt == "text"){
  // do stuff here
}
rybo111
  • 12,240
  • 4
  • 61
  • 70