Bug Tracker

Ticket #14858 (closed bug: fixed)

Opened 13 months ago

Last modified 12 months ago

jQuery 1.11.0 and up - .val() of option with no value attribute returning white-space

Reported by: hovenj@… Owned by: dmethvin
Priority: high Milestone: 1.11.1/2.1.1
Component: attributes Version: 1.11.0
Keywords: Cc:
Blocking: Blocked by:


Beginning with jQuery 1.11.0 (and continuing only in the 1.x branch), using .val() on an option with no value attribute returns the entire text of the option, including white-space.


  <option selected>

Length of selected option value: <span id="result"></span>

<script type="text/javascript">
 $r = $('#result'),
 $o = $('select option:selected');
$r.text( $o.val().length );

In 1.10.1 and 2.x (edge), this returns 0. In 1.11.0 and forward, this returns 12.

Change History

comment:1 Changed 13 months ago by anonymous

Reproduced in IE9 and Chrome 34.0.1847.45 beta-m, both on Windows 7.

I expected non-significant white-space to be excluded from the value. Based on other versions, you wouldn't even expect a single space.

comment:2 Changed 13 months ago by anonymous

Also reproduced in Firefox 25.0.1 on Windows 7 and IE 10 (.0.9200.16798) on Windows 8.

comment:3 Changed 13 months ago by anonymous

Val on options changed over to jQuery.text in the following commit:  https://github.com/jquery/jquery/commit/92cbf5362cfe4fe52e919eecfebe8ecbe27ee5e3

It looks like jQuery.text()'s behavior is consistent across these versions, so maybe this commit was bad?

Updated fiddle:  http://jsfiddle.net/LmCFA/2/

comment:4 Changed 13 months ago by anonymous

I've added a pull request based on my observations.


comment:5 Changed 13 months ago by dmethvin

  • Priority changed from undecided to high
  • Status changed from new to open
  • Component changed from unfiled to attributes
  • Milestone changed from None to 1.11.1/2.1.1

comment:6 Changed 13 months ago by dmethvin

  • Owner set to dmethvin
  • Status changed from open to assigned

comment:7 Changed 12 months ago by John Hoven

  • Status changed from assigned to closed
  • Resolution set to fixed

Attributes: Trim whitespace from option text when returned as a value

Fixes #14858 Ref #14686 Closes gh-1531 (cherry picked from commit 9ec429cf6270e455aba4eba85f4db80e633806b6)



Changeset: 541e7349b6533eb533c15d17e3e9e432e4a719ea

Note: See TracTickets for help on using tickets.