Skip to main content

Bug Tracker

Side navigation

#9319 closed bug (fixed)

Opened May 17, 2011 07:46PM UTC

Closed May 20, 2011 03:08PM UTC

Last modified May 20, 2011 04:24PM UTC

Default val() getter expects a String returned

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

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

Attachments (0)
Change History (9)

Changed May 17, 2011 08:14PM UTC by addyosmani comment:1

component: unfiledmanipulation
priority: undecidedlow
status: newopen

Changed May 17, 2011 08:27PM UTC by timmywil comment:2

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 ?
        "" :
        ret;

Changed May 17, 2011 08:33PM UTC by rwaldron comment:3

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

Changed May 17, 2011 09:12PM UTC by rwaldron comment:4

owner: → rwaldron
status: openassigned

Changed May 17, 2011 09:42PM UTC by rwaldron comment:5

keywords: → needsdocs

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.

http://api.jquery.com/val

Changed May 20, 2011 02:17PM UTC by rwaldron comment:6

#9362 is a duplicate of this ticket.

Changed May 20, 2011 03:08PM UTC by rwldrn comment:7

resolution: → fixed
status: assignedclosed

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

More Details:

Changeset: f82b9dddc498efdfbcd0978f8ba3e1da3b48eff3

Changed May 20, 2011 03:19PM UTC by rwaldron comment:8

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?

Changed May 20, 2011 04:24PM UTC by dmethvin comment:9

keywords: needsdocs