1

How can I get jQuery to throw an error when one of my selectors selects nothing?

3 Answers 3

2
if(!$('selector').length)
    throw error
Sign up to request clarification or add additional context in comments.

1 Comment

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!
0

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

0

I added an example on how to debug this here: jquery hides simple javascript errors

And here is a fiddle: http://jsfiddle.net/B4XQx/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.