Hide/show element with a boolean

Aakil Fernandes picture Aakil Fernandes · Jan 14, 2015 · Viewed 37.2k times · Source

I tend to have a lot of these in my code

if(shouldElementBeVisible)
    $element.show()
else
    $element.hide()

Is there any more elegant way packaged with javascript, jquery, or underscore? Ideally I want something that looks like this

$element.showOrHideDependingOn(shouldElementBeVisible)

Answer

Aakil Fernandes picture Aakil Fernandes · Jan 14, 2015

Apparently you can just pass a boolean to the toggle function

$element.toggle(shouldElementBeVisible)