jQuery 1.4.4+ includes this already, using DOMElement.getAttribute(), but for prior versions you can bind an element to the getData event. This event is fired internally by jQuery.fn.data if you attempt to retrieve data for a key that has no corresponding value in the data cache. The event handler can then be used to query the element.dataset store, or use getAttribute() as a fallback.
Here is an implementation that is added to the jQuery.fn object:
$.fn.bindDataAttributes = function() {
return this.each(function() {
$(this).bind('getData', function(ev, key) {
return this.dataset ? this.dataset[key] || undefined :
this.getAttribute('data-' + key.replace(/([a-z])([A-Z])/g, "$1-$2").toLowerCase()) || undefined;
});
});
};
Since the function returns this, you can use it in a chain, such as:
$("#myElem").bindDataAttributes().data('foo');
You can view a sample (barebones as it may be) here:
http://jsfiddle.net/adams_ea/2Mee2/