Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery exception if element doesn't exist

I working with jQuery and I need to get anytime and anywere exception (with any operation), if I attach some event or try to perform some action with elements (got from selector) that don't exist. Is there some internal "strict" mode in jQuery for this issue?

like image 779
Alex Ivasyuv Avatar asked May 11 '10 15:05

Alex Ivasyuv


2 Answers

No, there isn't.

However, you could make a simple plugin for it:

$.fn.checkEmpty = function() {
    if (!this.length)
        throw new Error("No elements matched by " + this.selector);
    return this;
};

$('...').checkEmpty().bind(...);

Alternatively:

function $s() { return $.apply(this, arguments).checkEmpty(); }

$s('...').bind(...);
like image 51
SLaks Avatar answered Sep 27 '22 21:09

SLaks


Check this post for ways to handle an "exists"

Exists in jquery

like image 45
Mark Schultheiss Avatar answered Sep 27 '22 20:09

Mark Schultheiss