jQuery plugin if else clause not working?
-
21-04-2021 - |
Question
I'm writing a jQuery plugin and I am trying to do this:
$(this).height>=options.maxHeight ?
$(this).css({overflow:"auto"}) :
$(this).css({overflow:"hidden"})
however it's not working, I have used this method in my javascript before.
This works how it should, just i was trying to use little code as possible.
if($(this).height()>=options.maxHeight)
{
$(this).css({overflow:"auto"});
}
else
{
$(this).css({overflow:"hidden"});
}
Solution
Change it to:
$(this).height() >= options.maxHeight ?
$(this).css({overflow:"auto"}) :
$(this).css({overflow:"hidden"})
You forgot the ()
in .height()
.
You could also use this:
$(this).css({overflow: $(this).height() >= options.maxHeight ? 'auto' : 'hidden'});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow