Greg Gum Greg Gum - 4 months ago 10
Javascript Question

If jquery does not find a match, is an error thrown?

Will an error be thrown if jquery does not find any items of class box-typical-body?

$('.box-typical-body').jScrollPane(jScrollOptions);


If so, then as far as I can tell, the correct code would be:

if ( $( ".box-typical-body" ).length ) {
$('.box-typical-body').jScrollPane(jScrollOptions);
}


I have tested, and it appears to be so, but am a novice at jquery and want to make sure.

Answer

$('.box-typical-body') will always return an object, its just an empty one, so the error throwing will actually depend on if the plugin jScrollPane check if the object is empty of not.

So its safer to do it your way to account for all possibilities.

Comments