Mooseman
Mooseman

Reputation: 18891

jQuery data selector not matching .data

I have a selector: $('div.data[data="true"]').not('div.data[data-loading="true"]').

I am using .data("data-loading","true") on the element.


Problem:

Because .data doesn't truly set an attribute like .prop would, the above .not selector does not match elements which I have set the above .data to. Is there a selector I can use the match data set by .data, or must I use .prop in addition to/instead of .data?

Upvotes: 0

Views: 401

Answers (1)

T.J. Crowder
T.J. Crowder

Reputation: 1075567

There are no selectors that match data you put on your element via the .data function. In your case, you can readily make that an attribute, though. Your selector is already looking for an attribute, so just change

.data("data-loading","true")

to

.attr("data-loading","true")

The div.data[data="true"] selector won't work unless there's an attribute on the div with the name data, which as you've observed, there isn't (and shouldn't be, it would be an invalid attribute).

If you need to mark the divs that have data stored on them, I would use a class for that. I'd probably also use a class for the data-loading flag.

Upvotes: 1

Related Questions