Ticket #9502 (closed enhancement: invalid)
provide .hasData() method for consistency
| Reported by: | shikakaa@… | Owned by: | shikakaa@… |
|---|---|---|---|
| Priority: | low | Milestone: | 1.next |
| Component: | data | Version: | 1.6.1 |
| Keywords: | Cc: | ||
| Blocking: | Blocked by: |
Description
In the data category, all methods (queue, dequeue, data and removedata) have a jQuery.method() and .method() version, except for hasdata.
hasData should also be available in the latter format.
jQuery(element).hasData(key) returns boolean, true if key data field is set on element.
this would be a convenient way to avoid reference errors and having to check the value.
Change History
comment:1 Changed 2 years ago by rwaldron
- Owner set to shikakaa@…
- Priority changed from undecided to low
- Status changed from new to pending
- Component changed from unfiled to data
comment:2 Changed 2 years ago by shikakaa@…
- Status changed from pending to new
I think I understand what you're saying, but your example is flawed - at least in chrome -, all tests return true but only the second should.
comment:3 Changed 2 years ago by dmethvin
- Status changed from new to closed
- Resolution set to invalid
jQuery.hasData() is *only* meant to determine if a data object exists on an element at all and says nothing about whether a particular data key exists inside the data object.
I don't think the described functionality is needed, and it's easy to add yourself if you want it -- or just check for the data being undefined. We certainly wouldn't want to call it .hasData() since that would be confusing.
comment:4 Changed 2 years ago by rwaldron
Look closer... 1 & 3 have negation operators at the front of the expression. #2 had double negation.
Here's a clearer example: http://jsfiddle.net/rwaldron/3EyaP/5/
Please follow the bug reporting guidlines and use jsFiddle when providing test cases and demonstrations instead of pasting the code in the ticket.

Without adding a single line of code to jQuery and only costing you 1 or 2 characters, instead of 3...
http://jsfiddle.net/rwaldron/3EyaP/