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.

Erin
  • 85
  • 2
  • 13
  • Thanks, I wasn't aware. – Erin May 16 '17 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. – George May 16 '17 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. – Erin May 16 '17 at 13:46

1 Answers1

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.

Ahmed Ginani
  • 6,522
  • 2
  • 15
  • 33
  • Unfortunately neither the 'jquery' nor the script in footer worked, thanks for the tips though :) – Erin May 16 '17 at 13:38
  • 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... – Erin May 16 '17 at 13:41
  • 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: https://developer.wordpress.org/reference/functions/wp_enqueue_script/ – Ahmed Ginani May 16 '17 at 13:44