0

I get this error message in the console log:

Uncaught TypeError: $ is not a function It references the following line: $('.bericht').append(themessage);

This is my full JS file:

var thehours = new Date().getHours();
var theminutes = new Date().getMinutes();
var themessage;
var open = ('nu open');
var gesloten = ('nu gesloten');

if (thehours === 9 && theminutes >= 30) { // 09:30 - 10:00 open
    themessage = open;

}   else if (thehours >= 10 && thehours < 18) { // 10:00 - 18:00 open
themessage = open;

}   else { // when we are not open - we are closed :)
themessage = gesloten;
}

$('.bericht').append(themessage);


var thehours1 = new Date().getHours();
var theminutes1 = new Date().getMinutes();
var themessage1;
var open1 = ('09.30 - 18.00');
var gesloten1 = ('18.00 - 09.30');

if (thehours1 === 9 && theminutes1 >= 30) { // 09:30 - 10:00 open
    themessage1 = open1;

}   else if (thehours1 >= 10 && thehours1 < 18) { // 10:00 - 18:00 open
    themessage1 = open1;

}   else { // when we are not open - we are closed :)
    themessage1 = gesloten1;
}


$('.bericht1').append(themessage1);

In my html site, the Javascript works perfectly and depending on the time of day I get a different message, but in my Wordpress site neither message appears.

Any idea why this is and what I can do to make it work?

Many thanks.

3
  • Thanks, I wasn't aware. Commented May 16, 2017 at 13:36
  • Neither was I until I googled Uncaught TypeError: $ is not a function and that answer was the first result. I don't want to sound rude but please do a little bit of research before asking a question next time. Commented May 16, 2017 at 13:37
  • Sorry I think I was googling the wrong thing focussing on the wordpress side of it, will try and google better next time. Commented May 16, 2017 at 13:46

1 Answer 1

1

Try to use jQuery instead of $ like:

jQuery('.bericht').append(themessage);

It should work.

If It will not work then just check in your view source to confirm that you haven't called jQuery library file multiple time and try to call the script in footer before </body> tag.

Hope it will work for you.

Thanks.

Sign up to request clarification or add additional context in comments.

3 Comments

Unfortunately neither the 'jquery' nor the script in footer worked, thanks for the tips though :)
Ah, wait! Putting jQuery (with a capital Q, I forgto that) in the footer works! However, I don't think you're supposed to put Javascript in the footer in Wordpress are you? I've been told it always needs to enqueued as a file...
Yes @Erin, you can enqueue as a file and put your code inside. you can set the last parameter as true in wp_enqueue_script to add the script in footer automatically. see this: developer.wordpress.org/reference/functions/wp_enqueue_script

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.