Skip to main content

Bug Tracker

Side navigation

#4275 closed enhancement (invalid)

Opened March 02, 2009 12:40AM UTC

Closed October 29, 2010 05:10PM UTC

Annotate selectors with "!" to mean "must match"

Reported by: skrul Owned by: john
Priority: low Milestone: 1.4
Component: selector Version: 1.3.2
Keywords: Cc:
Blocked by: Blocking:
Description

A suggestion from a new jQuery user: Several times I've run into problems where my selector expression was not matching due to a bug, but it was not obvious since it is perfectly valid to traverse and manipulate an empty jQuery object.

So I propose adding a syntax to the selector expression that means "if this does not match, throw an exception." I think the "!" symbol is appropriate since it reminds me of CSS's "!important".

For example, $("#myid!") would throw an exception if #myid was not found.

Thoughts?

Attachments (0)
Change History (4)

Changed March 02, 2009 01:20AM UTC by dmethvin comment:1

You could also accomplish this with a plugin:

jQuery.fn.orDie = function(msg){
  if ( !this.length)
    alert("Nothing selected! "+(msg||""));
  return this;
};

Changed March 08, 2009 10:03AM UTC by dantman comment:2

I don't like the idea of using !, a ! reminds me far to much of not.

$('#foo!.foo');

Changed August 09, 2009 01:18AM UTC by dmethvin comment:3

component: unfilledselector
owner: → john
priority: majortrivial

Changed October 29, 2010 05:10PM UTC by rwaldron comment:4

priority: triviallow
resolution: → invalid
status: newclosed

! is reserved for representing "not"