Bug Tracker

Opened 13 years ago

Closed 13 years ago

#811 closed bug (fixed)

"Elem has no properties" error when trying to use if (jQuery(this).attr('id') == null)

Reported by: tuupola@… Owned by:
Priority: major Milestone: 1.1
Component: core Version: 1.1
Keywords: Cc:
Blocked by: Blocking:

Description

With 1.0.4 it used to be possible to check if plugin was attached to nonexisting element by using:

if (jQuery(this).attr('id') == null) {
    return false;
};

This does not seem to work with 1.1. Instead "elem has no properties" is given. Live example at:

http://www.appelsiini.net/~tuupola/jquery/bugs/elemhasnoproperties-1.1.html

Change History (4)

comment:1 Changed 13 years ago by joern

The interesting question: What is worse? A thrown exception that can be easily handled by the user, or a swallowed exception, that the user can't see without hours or days of debugging?

I consider the example a programming error that should not be handled by jQuery.

Other opinions?

comment:2 Changed 13 years ago by Dave

It looks like a jQuery error to me. The plugin's code should never run if there is no element with id="nosuch".

$('#nosuch').foo();

The bug is that jQuery is selecting [ undefined ] instead of [ ] and it all goes downhill from there. It seems to be fixed in the most recent releases though.

comment:3 Changed 13 years ago by malsup

Regardless, that's some weird syntax. 'this' inside the plugin is the jQuery object so why not just write:

if (this.length == 0) return false;

comment:4 Changed 13 years ago by john

Milestone: 1.1
Resolution: fixed
Status: newclosed

Dave: It's physically impossible to stop a plugin from running if there's no matched elements. Even ignoring that fact, there are cases where you may want to have a method execute only if no matched elements are found. A hypothetical example:

$("div > p").none(function(){
   // Execute if none are found
});

That being said - this was an actual bug, and I just fixed it in SVN rev 1135.

Note: See TracTickets for help on using tickets.