Commit 626624a1 authored by Jacob Wright's avatar Jacob Wright Committed by jeresig

Fix bug when accessing .data() on an empty set. Return null rather than throwing exception.

parent 1f667aa0
...@@ -129,8 +129,8 @@ jQuery.extend({ ...@@ -129,8 +129,8 @@ jQuery.extend({
jQuery.fn.extend({ jQuery.fn.extend({
data: function( key, value ) { data: function( key, value ) {
if ( typeof key === "undefined" && this.length ) { if ( typeof key === "undefined" ) {
return jQuery.data( this[0] ); return this.length ? jQuery.data( this[0] ) : null;
} else if ( typeof key === "object" ) { } else if ( typeof key === "object" ) {
return this.each(function() { return this.each(function() {
......
...@@ -65,7 +65,10 @@ test(".data()", function() { ...@@ -65,7 +65,10 @@ test(".data()", function() {
var div = jQuery("#foo"); var div = jQuery("#foo");
div.data("test", "success"); div.data("test", "success");
same( div.data(), {test: "success"}, "data() get the entire data object" ) same( div.data(), {test: "success"}, "data() get the entire data object" );
var nodiv = jQuery("#unfound");
equals( nodiv.data(), null, "data() on empty set returns null" );
}) })
test(".data(String) and .data(String, Object)", function() { test(".data(String) and .data(String, Object)", function() {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment