Is there any good practice to avoid your jQuery codes that don't work?
For example:
$('.this #is:my(complexSelector)').doSomething();
I know that every time this line is executed, the selector is designed to match at least one element or a certain number of elements. Is there any standard or good way to test this?
I thought of something like this:
var $matchedElements = $('.this #is:my(complexSelector)'); if ($matchedElements.length < 1) throw 'No matched elements'; $matchedElements.doSomething();
I also think unit testing will be a valid option, not messing up the code.
My question may be silly, but I wonder if there is a better option than what I'm doing now or not. Also, maybe I'm wrong when checking if any element matches my selector. However, as the page continues to grow, selectors may stop matching some elements, and parts of the functionality may stop working unintentionally.
javascript jquery
Matias
source share