0

My jquery file is not working in WordPress.

The error is here:

enter image description here

My js code:

(function($) {
    $(document).ready(function(){
    $('#read_more').click(function() {
    $('.display_none').show().animate({'transition': '2s'});
   });
  });
})( jQuery );

I have an added custom-script.js file in functions.php like this:

function aristo_css_js(){
  wp_enqueue_script('myjs', get_template_directory_uri().'/assets/js/custom-script.js');
  wp_enqueue_script('myjs');
}
add_action('wp_enqueue_scripts','aristo_css_js');

What is the main problem for this was not working? But if I added custom script code before body tag it's working. ?

3
  • Make sure you're loading jQuery into the page. Commented Mar 4, 2019 at 7:22
  • Check the order of your scripts, chances are your own is loaded before jQuery. Commented Mar 4, 2019 at 7:24
  • Thanks I got the problem and it solved.@JackBashford Commented Mar 4, 2019 at 9:04

4 Answers 4

2

I solve my own question by add jquery file before my custom-script

wp_enqueue_script('jquery-main', get_template_directory_uri().'/assets/js/jquery-3.3.1.min.js');
wp_enqueue_script('myjs', get_template_directory_uri().'/assets/js/custom-script.js');
Sign up to request clarification or add additional context in comments.

Comments

0

I had this same issue once.

WordPress ships with its own version of the jQuery library.

Try using jQuery instead of just $ sign.

For example:

var svg = $("#svg-container");

should be replaced with

var svg = jQuery("#svg-container");

2 Comments

1. OP is already doing this (IIFE passing jQuery) 2. check the error message OP has posted: jQuery is not defined
Then double check the order of your scripts. Make sure the jquery script is before your custom script.
0

Try to use this updated javascript code,

jQuery.noConflict();
(function(jQuery) {
    jQuery(document).ready(function(){
    jQuery('#read_more').click(function() {
    jQuery('.display_none').show().animate({'transition': '2s'});
   });
  });
})( jQuery );

function aristo_css_js(){
 wp_enqueue_script('jquery-main', get_template_directory_uri().'/assets/js/jquery-3.3.1.min.js');
wp_enqueue_script('myjs', get_template_directory_uri().'/assets/js/custom-script.js');
}
add_action('wp_enqueue_scripts','aristo_css_js');

Thanks!

1 Comment

I already mentioned it on my question. I found the same error when I change with code.
0

I hope this will fix your issue

(function($) {

// Use $() inside of this function like so 

$(#selector)

})(jQuery);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.