Pennf0lio
Pennf0lio

Reputation: 3896

check for className before running the function

How can I achieve this?

for each pages I have attached a unique class-name so I can target them by css later.

I want to run a function but only targeting the homepage.

eg.

if($('body').hasClass('pageHome')) {
 callMe;
}

function callMe() {
 alert('I am Home!');
}

Upvotes: 1

Views: 42

Answers (3)

charlietfl
charlietfl

Reputation: 171669

Concept should work fine as long as you are wrapping code in

$(function(){  /* run code*/ })

and you need to add "()" to callme();

Upvotes: 0

Adam Rackis
Adam Rackis

Reputation: 83356

It looks like you're close. To call callMe, you'll want parenthesis to indicate that it's a function call:

if($('body').hasClass('pageHome')) {
 callMe();
}

Upvotes: 1

Jasper
Jasper

Reputation: 76003

Your forgot the parenthesis when you called callMe:

function callMe() {
    alert('I am Home!');
}
if($('body').hasClass('pageHome')) {
    callMe();
}

Does that help?

Upvotes: 1

Related Questions