views:

28

answers:

1

Is there any way to globally monitor or listen to if a selection is made in jQuery and it returns no elements? E.g.:

You have a page with:

<div id=”some-id”></div>

And in a JavaScript someone tries to get the element but mistypes the id:

$(“#someid”)

Is there any way to globally handle when a jQuery selection returns no elements?

+1  A: 

No. You can verify this by looking at the source that handles that case. Look into (in this case) jquery-1.4.2.js and search for the line:

                // HANDLE: $("#id")

You will see that there is no callback mechanism. It just returns if there is no result. However, the result object will always have a length (or .size()) of 0, so you can test individual results.

Your options if you want this are either to do all queries on the page with a custom function, or modify jQuery. Unfortunately, because the central function (jQuery.fn.init) is so long and has multiple purposes, there isn't a good way to do this other than to modify the jQuery file itself.

Jesse Millikan