Bug Tracker

Ticket #9319 (closed bug: fixed)

Opened 3 years ago

Last modified 3 years ago

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

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

Change History

comment:1 Changed 3 years ago by addyosmani

  • Priority changed from undecided to low
  • Status changed from new to open
  • Component changed from unfiled to manipulation

comment:2 Changed 3 years ago by timmywil

  • Component changed from manipulation to attributes

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;

comment:3 Changed 3 years ago by rwaldron

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

comment:4 Changed 3 years ago by rwaldron

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

comment:5 Changed 3 years ago by rwaldron

  • 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.

 http://api.jquery.com/val

comment:6 Changed 3 years ago by rwaldron

#9362 is a duplicate of this ticket.

comment:7 Changed 3 years ago by rwldrn

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

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

More Details:

comment:8 Changed 3 years ago by rwaldron

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 3 years ago by dmethvin

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