Skip to main content

Bug Tracker

Side navigation

#811 closed bug (fixed)

Opened January 15, 2007 05:29PM UTC

Closed January 20, 2007 03:59AM UTC

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

Reported by: tuupola@appelsiini.n 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

Attachments (0)
Change History (4)

Changed January 17, 2007 01:30PM UTC by joern comment:1

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?

Changed January 17, 2007 02:28PM UTC by Dave comment:2

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.

Changed January 17, 2007 02:51PM UTC by malsup comment:3

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; 

Changed January 20, 2007 03:59AM UTC by john comment:4

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.