Keith Keith - 6 months ago 10
Javascript Question

Data Selector With jQuery Not Working

I've tried to create a JSFiddle for an example of a selector I was trying to use and it's not working. Can someone eyeball it and tell me what I have wrong?

Here's the fiddle:
https://jsfiddle.net/j3cmo4ow/4/

My code was inspired by this example:
http://api.jqueryui.com/data-selector/

$(":data(date)").each(function() {
var element = $(this);
element.css("backgroundColor", element.data("color"));
});

Answer

You want an attribute selector:

$("[data-date]")

Fiddle: https://jsfiddle.net/j3cmo4ow/5/

If you want the :data pseudo selector to work, you need to include jQuery UI.

Comments