Dandy
Dandy

Reputation: 861

Checking styles before changing?

I have a menu and I'm showing some content only if the user is on the home page. This condition runs every time any menu item is clicked. If the user clicks on the other menu items I do not want to display this content. I'm trying to prevent the JavaScript from having to go back and forth from the HTML. Is this excessive?

Example:

if(pageNumber === home) {
    $('#content').css({'display':'block'});
} else if($('#content').css('display') !== 'none') {
    $('#content').css({'display':'none'});
}

Upvotes: 1

Views: 44

Answers (1)

colestrode
colestrode

Reputation: 10658

You can just use the toggle method:

$('#content').toggle(pageNumber === home);

http://api.jquery.com/toggle/

Upvotes: 3

Related Questions