Bug Tracker

Opened 9 years ago

Closed 9 years ago

#14858 closed bug (fixed)

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:
Blocked by: Blocking:

Description

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.

http://jsfiddle.net/LmCFA/1/

<select>
  <option selected>
        
  </option>
</select>

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

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

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

Change History (7)

comment:1 Changed 9 years 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 9 years 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 9 years 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 9 years ago by anonymous

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

https://github.com/jquery/jquery/pull/1531

comment:5 Changed 9 years ago by dmethvin

Component: unfiledattributes
Milestone: None1.11.1/2.1.1
Priority: undecidedhigh
Status: newopen

comment:6 Changed 9 years ago by dmethvin

Owner: set to dmethvin
Status: openassigned

comment:7 Changed 9 years ago by John Hoven

Resolution: fixed
Status: assignedclosed

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

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

Conflicts:

src/attributes/val.js

Changeset: 541e7349b6533eb533c15d17e3e9e432e4a719ea

Note: See TracTickets for help on using tickets.