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.

4
  • This is driving me crazy too... did you find any solution or did you just forget about it?
    – Mischa
    Jan 14, 2012 at 16:20
  • @Mischa - I just let it go for the time being :(
    – malonso
    Jan 17, 2012 at 11:51
  • I guess it's a bug in Firefox. It seems to be happening only with selects. What a waste of time...
    – Mischa
    Jan 17, 2012 at 13:50
  • 1
    Right now I only see this in Google Chrome's latest version, not Firefox's. Wonder why?
    – trysis
    Apr 26, 2014 at 0:42

5 Answers 5

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

2
  • The ticket 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 6, 2013 at 21:53
  • I am using $(this).get(0).value for now till they sort this out Apr 18, 2014 at 3: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 ;-)

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 :)

5
  • 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 5, 2011 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 5, 2011 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, 2011 at 12:28
  • Simpler code, but still produces the same warning (FF10 error console).
    – pelms
    Feb 2, 2012 at 17:15
  • what if i want .text() of the selected option
    – mike nvck
    Apr 10, 2014 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>
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.