-2

I need to get the value of an option when it has an attribute selected="selected".

<option class="level-0 sf-item-33" value="vodafone" selected="selected" data-sf-cr="_sft_33" data-sf-hide-empty="0">Vodafone</option>

<option class="level-0 sf-item-33" value="year" selected="selected" data-sf-cr="_sft_33" data-sf-hide-empty="0">2012</option>

<option class="level-0 sf-item-33" value="ogilvy" selected="selected" data-sf-cr="_sft_33" data-sf-hide-empty="0">Ogilvy</option>

I then need this text value to be inserted like:

$('.Agency .dropdown-toggle').html(MY_VALUE_TEXT_1).append(' <span class="caret"></span>');
$('.Client .dropdown-toggle').html(MY_VALUE_TEXT_2).append(' <span class="caret"></span>');
$('.Year .dropdown-toggle').html(MY_VALUE_TEXT_3).append(' <span class="caret"></span>');
rob.m
  • 9,843
  • 19
  • 73
  • 162
  • Your edit absolutely doesn't help to understand your question. Provide online sample to replicate your issue as a jsFiddle and with all relevant code in question, thx! – A. Wolff Jan 19 '15 at 17:53
  • @A.Wolff hey ma check the example here: http://tinyurl.com/kxohmwj see that option with a value? that text should be the same as per the text on the last item on the top right nav. Basically the top right nav should reflect whatever is in the option – rob.m Jan 19 '15 at 18:03
  • So you need to get URL parameters (if any) on page load once user submit the FORM (and not on SELECT onchange event) and set specific anchor tags value in nav bar. See how to get parameters e.g: http://stackoverflow.com/questions/979975/how-to-get-the-value-from-the-url-parameter Because, on submiting FORM the page is reloaded so no option is selected... Or use ajax to handle all the logic without reloading the page. Anyway, this is too broad as question... And i'd not be suprised to still not understand your question... – A. Wolff Jan 19 '15 at 18:12
  • my page is keeping the attribute selected="selected" once the page is reloaded and i need to check the value or text of that selected="selected" option and grab its text – rob.m Jan 19 '15 at 18:14
  • Oh ya, i've checked using the years one which seems to be reseted on each load. EDIT: after a retry, seems to keep it – A. Wolff Jan 19 '15 at 18:16
  • yes so you see what i mean now? apologies if i couldn't explain it better – rob.m Jan 19 '15 at 18:17

2 Answers2

1

If you use jQuery:

$('option :selected').text();

grindking
  • 917
  • 5
  • 13
  • 29
0

You shouldn't try to tie the event directly to the option. Use the change event of the drop down.

$("#ddlMyOptions").change(function() {
    $("#divSelections").append("Selected: " + $("#ddlMyOptions").val() + "<br>");
});

JSFiddle example: http://jsfiddle.net/dddh17ka/

Spencer Ruport
  • 34,865
  • 12
  • 85
  • 147
  • I don't need it on a click, I need to get it once the page is already loaded and the option is selected – rob.m Jan 19 '15 at 17:45
  • Do you need it when the page is loaded or when the option is selected? Those are two different events. – Spencer Ruport Jan 19 '15 at 17:46
  • they are not different. if i select an option and I do a search, then the page which will load will have that option as selected – rob.m Jan 19 '15 at 17:47
  • @rob.m - If you think page load and option select are the same event I really can't help you. – Spencer Ruport Jan 19 '15 at 17:49
  • i don't think they are the same. Don't get me wrong, I am not talking about jQuery .load. I am saying that when the search it finished, you get a page printed out with the option selected.. – rob.m Jan 19 '15 at 17:50
  • 3
    @rob.m - The problem is you're using a combination of terms incorrectly which makes it impossible to determine what you're asking. – Spencer Ruport Jan 19 '15 at 17:51
  • @SpencerRuport Exactly, cunfusing question even i guess the answer is quite simple... – A. Wolff Jan 19 '15 at 17:52
  • Why is my question confusing? I haven't said I needed to get the text on a on.("change".. i simply asked how to get a value form an option with select into another text value of an element. – rob.m Jan 19 '15 at 17:53
  • 1
    The more you comment, the less i understand it, i'm afraid. `get a value form an option with select into another text value of an element`??? – A. Wolff Jan 19 '15 at 17:55
  • nevermind, I did update my question. Thanks anyway tho – rob.m Jan 19 '15 at 17:55
  • @rob.m - "i simply asked how to get a value form an option with select into another text value of an element" We don't understand what that means. There are two events that could handle this. Page load or select change. You've told us neither one is what you want so we're at a loss. You need to walk through step by step everything the user and the browser do that lead up to the triggering of this behavior. – Spencer Ruport Jan 19 '15 at 17:57
  • @SpencerRuport no no, i believe it's just me and the way I explained things. Believe me it is much simpler and i'll make you smile once you get what i mean. Check the example url, see the option as one as selected? Well see now the top rigth nav? That value text should reflect what the top menu says. At the moment you can see the top menu last item is without text http://tinyurl.com/kxohmwj – rob.m Jan 19 '15 at 18:01
  • @rob.m - I can't visit your site. Like I said you need to break down the problem into user and browser actions and identify all the situations where the value should be updated. Only then will someone be able to offer a solution. – Spencer Ruport Jan 19 '15 at 18:09