Skip to main content

Search and Top Navigation

#5403 closed bug (notabug)

Opened March 22, 2010 08:25PM UTC

Closed March 23, 2010 04:22AM UTC

Last modified January 10, 2014 03:44PM UTC

:data selector fails to return results when the data is set to empty string

Reported by: neonskimmer Owned by:
Priority: major Milestone: none
Component: ui.core Version: 1.8
Keywords: :data, selector Cc:
Blocked by: Blocking:
Description

The :data additional selector in jquery.ui.core.js does not match elements that contain empty strings as data.

HTML:

    <ol id="test">
      <li id="hasdata">hello</li>
      <li>foobar</li>
    </ol>

CODE:

$("#hasdata").data("empty", "");
$("#hasdata").data("hello", "oh hai");

console.debug('has data:', $("#hasdata").data() );
        
var cant_find = $("#test").find("li:data(empty)");
console.debug("selector fails to see the empty string in the data", cant_find);
        
var can_find = $("#test").find("li:data(hello)");
console.debug("selector is cool with a non empty string", can_find);

This is likely due to this in ui.core.js, line 181:

	data: function(elem, i, match) {
		return !!$.data(elem, match[3]);
	}

Because "empty string" evaluates to false, !!"" returns false, even though the key exists in the data. Should probably be something like

 !($.data(elem, match[3]) === undefined)
, depending on how $.data works.

Attachments (0)
Change History (5)

Changed March 23, 2010 04:22AM UTC by scottgonzalez comment:1

resolution: → invalid
status: newclosed

This is working as intended.

Changed October 11, 2012 09:15PM UTC by scottgonzalez comment:2

milestone: TBD

Milestone TBD deleted

Changed February 07, 2013 01:44AM UTC by scottgonzalez comment:3

#9053 is a duplicate of this ticket.

Changed January 10, 2014 03:31PM UTC by Rycochet comment:4

This may be an old ticket, but it's a valid bug - it also hits on "false", "0", and anything else that evaluates as false - however jQuery.hasData() will in fact return true on all those cases, and only returns false when there isn't any data set.

As a rather obvious question - what happens when you want to store an array index in .data without modifying it...

Changed January 10, 2014 03:44PM UTC by scottgonzalez comment:5

milestone: → none

Replying to [comment:19 Rycochet]:

This may be an old ticket, but it's a valid bug

It's not a valid bug because it's not designed for generic values. Feel free to file a feature request to change the intent of the selector.

As a rather obvious question - what happens when you want to store an array index in .data without modifying it...

I'm not sure what you mean by "store an array index in .data without modifying it." Without modifying what?