How can I get jQuery to throw an error when one of my selectors selects nothing?
3 Answers
if(!$('selector').length)
throw error
1 Comment
Dov Kaufmann
I already have a selector (many of them, actually) but some of them do not work. I want to know when they are not selecting anything. Thanks!
I've created an example with Wikipedia: http://jsfiddle.net/Lctjs/. I want the first line to throw an error because it selects nothing. The other two lines actually do something (there is actually an element with class id "content") but the first line doesn't select anything to operate and I'd like to know. Thanks!
Comments
I added an example on how to debug this here: jquery hides simple javascript errors
And here is a fiddle: http://jsfiddle.net/B4XQx/