-3

how to get the selected text of the option.

<select class="float-left groupNames" name="Group">
       <option value="preview[object Object]0">test</option>
        <option value="preview[object Object]1">test1</option>
        <option value="preview[object Object]2">test2</option>
    </select>

I tried $(".groupNames option:selected").text(); but its not the right value. so I am getting testtest2 if I am selecting the third option.

XXDebugger
  • 1,581
  • 3
  • 26
  • 48
  • seems to work, it selects the selected option (without setting a selected, selected is the first option), can you show us what you have exactly? – depperm Jun 25 '15 at 00:26
  • *"but its not the right value"* And how should *we* know what the right value is supposed to be? – Felix Kling Jun 25 '15 at 00:30
  • @FelixKling It seems he/she wants to get the `value` but the above snippet returns the `textContent`. – Ram Jun 25 '15 at 00:32
  • @Vohuman: Probably. But there are other possibilities as well. Just saying. It's hard to help without knowing what the desired outcome is. – Felix Kling Jun 25 '15 at 00:32
  • @FelixKling Yes, exactly. Also the value attributes look suspicious! – Ram Jun 25 '15 at 00:34
  • 1
    Regarding your update: It seems like you have multiple `.groupNames` elements. That's the only explanation for getting this output. You have to select the one you want to get the option text from. Please provide a **complete** example that reproduces the issue. You are not new to Stack Overflow, you should know how this works! – Felix Kling Jun 25 '15 at 00:37
  • you got it right. I have 2 groupNames element.how to select the one I want. this is the second one in the HTML. – XXDebugger Jun 25 '15 at 00:42
  • https://api.jquery.com/eq-selector/ – Felix Kling Jun 25 '15 at 00:43

2 Answers2

2

$(".groupNames").val() is all you need.

jsFiddle example

However if you have multiple .groupNames elements, you'll need to be more specific with the selector.

$(".groupNames option:selected").text(); as you probably saw, will get the selected option's text, not the select's value.

j08691
  • 204,283
  • 31
  • 260
  • 272
0

hi please add an id to easily access the select here is an tested fiddle http://jsfiddle.net/elviz/259m3qkb/1/

    $("#newSkill").change(function(){
         var kamote = document.getElementById("newSkill").options[document.getElementById('newSkill').selectedIndex].text;
       alert(kamote);
     });
knives22
  • 303
  • 1
  • 2
  • 13