JakeFromStateFarm JakeFromStateFarm - 1 year ago 91
Javascript Question

I can't get my jquery to work in my wordpress site

I was told that jquery was automagically included in wordpress from the get go, but I went ahead and included jquery in my functions.php. I'm trying to do a simple script that scrolls down to a div#id when a link is clicked.

I created this script:

jQuery(document).ready(function($) {
jQuery("#view-visibility").click(function() {
jQuery('html, body').animate({
scrollTop: jQuery("#visibility").offset().top
}, 2000);

I put the script in my own file, script.js, and added it to the functions.php:


function theme_js() {
//parameters: 1.handle, 2.path, 3.array of dependents, 4.version specification, 5.load in footer? true or false
wp_enqueue_script( 'jquery', 'https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js', '', '3.1.1', true );
wp_enqueue_script( 'bootstrap_js', 'https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.4/js/bootstrap.min.js', array('jquery'), '', true );
wp_enqueue_script( 'my_js', get_template_directory_uri() . '/script.js', array('jquery'), '', true );


add_action( 'wp_enqueue_scripts', 'theme_js' );`

I still can't get it to work... I don't know what the deal is!! Should I be loading jquery in the head or before the body?

Link to my site

Answer Source

Learn to use the developer tools in Firefox (or Firebug) or Chrome or Safari or IE to check for Javascript and other console errors.

In the console, I see SyntaxError: Unexpected token ')' on line 9 of script.js.

So try }); });

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download