Ticket #11316 (closed enhancement: fixed)
Consider looking through valHooks by element type first, then by nodeName instead of the other way around
| Reported by: | mathias | Owned by: | mathias |
|---|---|---|---|
| Priority: | low | Milestone: | 1.7.2 |
| Component: | attributes | Version: | 1.7.1 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description (last modified by mathias) (diff)
jQuery#val contains this line of code: http://jsapi.info/jquery/1.7.1/val#L2317
hooks = jQuery.valHooks[ elem.nodeName.toLowerCase() ] || jQuery.valHooks[ elem.type ];
This is later repeated on line 2361: http://jsapi.info/jquery/1.7.1/val#L2361.
How about switching these around, making it:
hooks = jQuery.valHooks[ elem.type ] || jQuery.valHooks[ elem.nodeName.toLowerCase() ];
This would allow plugin authors to set e.g. jQuery.valHooks.input without affecting the valHooks for checkbox etc. Here’s a more detailed use case: https://github.com/mathiasbynens/jquery-placeholder/issues/52
Note that there’s only one item in jQuery.valHooks that’s based on the node name: jQuery.valHooks.select. All others are element types.
Change History
comment:2 follow-up: ↓ 4 Changed 16 months ago by timmywil
- Priority changed from undecided to low
- Status changed from new to open
- Type changed from feature to enhancement
- Component changed from unfiled to attributes
- Milestone changed from None to 1.7.2
I agree with this change.
As for your comment, the variable value would not be appropriate, but the string "value" doesn't need to be passed there either. It is there for convenience (a reference to the property name being retrieved), but is not used in any of the internal hooks. I think I did that right after writing attrHooks, which always passes the attribute name. We could remove it.
comment:4 in reply to: ↑ 2 Changed 16 months ago by mathias
Replying to timmywil:
I agree with this change.
Glad to hear! (Also very happy that you set milestone to 1.7.2 — yay!)
As for your comment, the variable value would not be appropriate, but the string "value" doesn't need to be passed there either. It is there for convenience (a reference to the property name being retrieved), but is not used in any of the internal hooks. I think I did that right after writing attrHooks, which always passes the attribute name. We could remove it.
Oh, I see! The second argument confused me and made me think of set instead. I was just wondering :)
comment:5 Changed 16 months ago by mathias
As discussed on IRC, it looks like jQuery.valHooks.button’s get and set take two and three arguments, respectively. For that reason it’s probably best to just leave the "value" stuff in there.
Pull request: https://github.com/jquery/jquery/pull/678
comment:6 Changed 16 months ago by dmethvin
Should we consider this a breaking change? If so it should be deferred to 1.8. I honestly doubt there are a lot of external hooks but people are always surprising us.
comment:7 Changed 16 months ago by timmywil
- Owner set to mathias
- Status changed from open to assigned
- Milestone changed from 1.7.2 to 1.8
I highly doubt it will cause a raucous, but I suppose since we're in beta, we should push to 1.8. Thanks Dave.
comment:8 Changed 15 months ago by Mathias Bynens
- Status changed from assigned to closed
- Resolution set to fixed
Fix #11316. Find valHooks first by element type, then by nodeName.
Reverses the previous search order.
Changeset: 2803a5e6f2704691fdd8ce4d34fe961d0192a0fb
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Related question: why is "value" (the string literal) used here instead of value (the variable name)? http://jsapi.info/jquery/git/val#L2372
if ( hooks && "get" in hooks && (ret = hooks.get( elem, "value" )) !== undefined ) { return ret; }