Why is my jquery val() not picking the actual selected value from my select box?

javascript, jquery

Solution

It's because in the first two cases, you invoked the $.val() function, but in the last case, you didn't.

$.val() returns the value of an HTML element. $() returns an object representing one or more html elements, with a number of methods you can invoke, including $.val()

Update: It appears that according to the documentation (http://api.jquery.com/attribute-contains-selector/), the value to the right of `id*=` is supposed to be enclosed in quotation marks. Does that make any difference?

Problem

When I type `$('select[id*=lstAdvGradYear]')` chrome gives me ``` <select name="Views\ContentArea$ctl00$lstAdvGradYear" id="Views\ContentArea_ctl00_lstAdvGradYear" class="ddReplace" style="display: none; "> <option value="2016">2016</option> <option value="2015">2015</option> <option value="2014">2014</option> <option selected="selected" value="2013">2013</option> <option value="2012">2012</option> </select> ``` When I type `$('select[id*=lstAdvGradYear]').val()` chrome replies with `"2016"`. When I type `$('select[id*=lstAdvGradYear] option:selected ').val()` chrome also replies with `"2016"` When I type `$('select[id*=lstAdvGradYear] option:selected')` chrome replies with `[<option value="2016">2016</option>]` What am I doing wrong? I have verified that there are no other drop downs with a similar name EDIT: To be clear, my issue is that `.val()` should be returning 2013 NOT 2016

Original source