27

Probably missing something pretty obvious but I can't figure out what is going on. I am trying to use jquery to determine the currently selected option in a dropdown (See fiddle) but when I do something like the following I get a Warning in the (FF9) console.

var selectedValue=$('#testSelect option:selected').val();

Warning Message:

Warning: Use of attributes' specified attribute is deprecated. It always returns true.

Am I doing something wrong? Is this something I should be concerned with? Thanks in advance.

malonso
  • 2,247
  • 1
  • 21
  • 33

5 Answers5

29

jquery is referencing the "specified" property on an Attr object, this is depreciated with Firefox 7, and always returns true. see https://developer.mozilla.org/En/DOM/Attr

i've raised a jquery ticket for this: http://bugs.jquery.com/ticket/11397

glob
  • 2,960
  • 17
  • 21
  • The [ticket](http://bugs.jquery.com/ticket/12072) is shown as "closed bug: fixed". Do I have to change anything in my code or will the next jQuery version just "fix" this? Perhaps I have to update my jQuery version used? – Priednis May 06 '13 at 21:53
  • I am using $(this).get(0).value for now till they sort this out – fullstacklife Apr 18 '14 at 03:38
2
$(document).on('change','select#FIELD_NAME', function() {
    alert('your selection was: '+$('select#FIELD_NAME').attr('value'));
    return false;
});

K.I.S.S. ...whenever it's possible ;-)

geekbuntu
  • 851
  • 6
  • 4
0

If the id #testSelect is your select name.

Get the value:

var selectedValue=$('#testSelect').attr('value');

Set the select value:

$('#testSelect').attr('value',your value);
sjbwylbs
  • 151
  • 14
0

Ask the select tag for it's value, it knows which one is selected and will use that tag for it's current value.

$('#testSelect').val()

Check it: http://jsfiddle.net/Ndzvm/1/

Sometimes it's simpler than you think it is :)

Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • 6
    That's how it should be done, but doesn't really answer the question regarding the warning message. This also produces the same warning msg in the console. – Shawn Chin Dec 05 '11 at 18:21
  • Chrome actually seems to issue no warning in either version for me. I dont think that message is coming form jQuery. But perhaps jQuery is just doing something that Firefox doesn't like. At the end of that day warnings are just warnings, and if it works in all target environments then I wouldn't worry about it too much. – Alex Wayne Dec 05 '11 at 18:26
  • 2
    @Alex - Sorry for my delay in responding to this. As Shawn mentions, the method you described is valid but my question/concern was about the warning FF is throwing. My apologies, the reason I used "#testSelect option:selected" instead of simply "#testSelect" was b/c I was ripping my hair out trying to figure out if any derivation of the selector would get rid of the warning message; it did not :( – malonso Dec 28 '11 at 12:28
  • Simpler code, but still produces the same warning (FF10 error console). – pelms Feb 02 '12 at 17:15
  • what if i want .text() of the selected option – mike nvck Apr 10 '14 at 22:37
0

can you use this code

<script type="text/javascript">
$(document).ready(function() {
$('select[id$=<%=DropDownList1.ClientID%>]').bind("keyup
change", function() {
if ($(this).val() != "")
$('#message').text("Text: " + $(this).
find(":selected").text()
+ ' Value: ' + $(this).val());
else
$('#message').text("");
});
});
</script>
Saeed-rz
  • 1,435
  • 1
  • 20
  • 38