Patterns for avoiding jQuery silent fails
<p>Is there any good practice to avoid your jQuery code silently fail? </p>

<p>For example: </p>

<pre><code>$('.this #is:my(complexSelector)').doSomething();
</code></pre>

<p>I know that every time this line get executed, the selector is intended to match at least one element, or certain amount of elements. Is there any standard or good way to validate that?</p>

<p>I thought about something like this:</p>

<pre><code>var $matchedElements = $('.this #is:my(complexSelector)');
if ($matchedElements.length &lt; 1)
    throw 'No matched elements';
$matchedElements.doSomething();
</code></pre>

<p>Also I think unit testing would be a valid option instead of messing the code.</p>

<p>My question may be silly, but I wonder whether there is a better option than the things that I'm currently doing or not. Also, maybe I'm in the wrong way checking if any element match my selector. However, as the page continues growing, the selectors could stop matching some elements and pieces of functionality could stop working inadvertently. </p>
