Bug Tracker

Opened 10 years ago

Closed 9 years ago

#4275 closed enhancement (invalid)

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?

Change History (4)

comment:1 Changed 10 years ago by dmethvin

You could also accomplish this with a plugin:

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

comment:2 Changed 10 years ago by dantman

I don't like the idea of using !, a ! reminds me far to much of not. $('#foo!.foo');

comment:3 Changed 10 years ago by dmethvin

Component: unfilledselector
Owner: set to john
Priority: majortrivial

comment:4 Changed 9 years ago by Rick Waldron

Priority: triviallow
Resolution: invalid
Status: newclosed

! is reserved for representing "not"

Note: See TracTickets for help on using tickets.