Fixes weird jquery bug

When next returns null, the hasClass further throws Untyped exception.

The fix checks for next first then follows the same logic.
Merge pull request #172 from dax70/master

Fixes weird jquery bug