Dov Kaufmann
Dov Kaufmann

Reputation: 11

jQuery error on null selector

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

Upvotes: 1

Views: 337

Answers (3)

David Hellsing
David Hellsing

Reputation: 108482

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

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

Upvotes: 0

Dov Kaufmann
Dov Kaufmann

Reputation: 1

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!

Upvotes: 0

Pramendra Gupta
Pramendra Gupta

Reputation: 14873

if(!$('selector').length)
    throw error

Upvotes: 2

Related Questions