Bug Tracker

Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#9319 closed bug (fixed)

Default val() getter expects a String returned

Reported by: Peter Beverloo Owned by: Rick Waldron
Priority: low Milestone: 1.next
Component: attributes Version: 1.6.1
Keywords: Cc:
Blocked by: Blocking:


Code (https://github.com/jquery/jquery/blob/master/src/attributes.js#L168):

return (elem.value || "").replace(rreturn, "");

This code assumes that elem.value will return a String. This is true for most form-related elements, but <meter> and <progress> (both of which have @value and are form-associated) define their value-property as a double. Doubles do not have a replace method, resulting in a JavaScript error.

Reproduction code:

var meterElement = document.createElement ('meter'),
    jqMeter = $ (meterElement);

meterElement.value = .5;
jqMeter.val ();

Tested in Google Chrome 13 with jQuery 1.6.1.

Two proposed fixes:

1) Force elem.value to be a string:

return ((elem.value || "") + "").replace(rreturn, "");

2) Add valHooks for both <meter> as <progress> doing the cast:

jQuery.each([ "meter", "progress" ], function() {
    jQuery.valHooks[ this ] = {
        get: function( elem ) {
            // The meter and progress elements define .value as a double
            return elem.value + "";

The issue was identified by Wilfred Nas, http://twitter.com/#!/wnas/status/70527764922499074

Change History (9)

comment:1 Changed 12 years ago by addyosmani

Component: unfiledmanipulation
Priority: undecidedlow
Status: newopen

comment:2 Changed 12 years ago by timmywil

Component: manipulationattributes

If value should be a number, we should probably leave it as a number. Maybe something like:

ret = elem.value;
return typeof ret === "string" ?
    ret.replace(rreturn, "") :
    ret == null ?
        "" :

comment:3 Changed 12 years ago by Rick Waldron

For more information about "meter" and "progress" http://peter.sh/examples/?/html/meter-progress.html

comment:4 Changed 12 years ago by Rick Waldron

Owner: set to Rick Waldron
Status: openassigned

comment:5 Changed 12 years ago by Rick Waldron

Keywords: needsdocs added

After discussing this, timmywil and I agree that returning a number is the right thing to do and that type "number" should be added to the list of returns.


comment:6 Changed 12 years ago by Rick Waldron

#9362 is a duplicate of this ticket.

comment:7 Changed 12 years ago by rwldrn

Resolution: fixed
Status: assignedclosed

Landing pull request 382. Adds support for number values (meter,progress); Fixes #9319.

More Details:

comment:8 Changed 12 years ago by Rick Waldron

I added "Number" to the docs entry:

entry type='method' name="val" return="String, Number, Array"

Someone with a better handle on the docs might want to add an example?

comment:9 Changed 12 years ago by dmethvin

Keywords: needsdocs removed
Note: See TracTickets for help on using tickets.