0

I've been trying to add my custom jQuery script to wordpress, the script code and all is working on jsfiddle when I choose the jquery library, but when I'm trying to add it on wordpress it is not working. Here's how added the wp enqueue code:

function cb_scroller() {

//wp_enqueue_script('jquery');

wp_register_script( 'scroller', get_template_directory_uri() . '/js/scroller.js', array('jquery'),'',true  );

wp_enqueue_script( 'scroller' );
}

add_action( 'wp_enqueue_scripts', 'cb_scroller' );

So what may be the problem? here's the jsfiddle attempt: https://jsfiddle.net/naimelhajj/q4bdfcwb/ (disregard the styling, I've imported the css and js as "external resources")

2
  • Try to take a look in console log if you do not have any errors with $ sign. If this is the case change all the code from scroller.js from $ sign to use jQuery instead. Instead of $(".arrow-left") you will have this jQuery(".arrow-left") and so on for other selectors Commented May 28, 2015 at 21:12
  • at what time is the action added? it is possible it is added at a time where the scripts have already been enqueued. this is not specified. Also look for any errors in your browser Commented May 28, 2015 at 21:14

1 Answer 1

2

This is your script (which you should have posted in the question)

$(document).ready(function(){
    $(".arrow-left").click(function(){
        $(".site-main-gluten").animate({scrollLeft: "-="+100});
    });
    $(".arrow-right").click(function(){
        $(".site-main-gluten").animate({scrollLeft: "+="+100});
    });        
});

Wordpress is in no-conflict mode by default, which means $ is not defined, it has to be

jQuery(document).ready(function($){
    $(".arrow-left").click(function(){
        $(".site-main-gluten").animate({scrollLeft: "-="+100});
    });
    $(".arrow-right").click(function(){
        $(".site-main-gluten").animate({scrollLeft: "+="+100});
    });        
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.