-1

This question already has an answer here:

I had the following jQuery script on a static HTML page and it worked fine. However, when I enqueue it into WordPress it doesn't work.

Here is the JS file.

$(window).scroll(function()
  if ($(".navbar").offset().top > 50) {
     $(".navbar-fixed-top").addClass("top-nav-collapse");
  } else {
     $(".navbar-fixed-top").removeClass("top-nav-collapse");
  }
)};

And here is the functions.php

function hat_rack_media_scripts() {
   //THEME CUSTOM STYLES AND SCRIPTS
   wp_enqueue_style( 'hat-rack-media-style', get_stylesheet_uri() );
   wp_register_script( 'theme-script', get_template_directory_uri() . '/js/script.js', array('jquery'), '1', true );
    if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
    wp_enqueue_script( 'comment-reply' );
    }
}
add_action( 'wp_enqueue_scripts', 'hat_rack_media_scripts' );

I feel like i included the file correctly, it is mapped to the right location, and it worked just fine in a static html page.

Thanks.

marked as duplicate by fuxia Aug 6 '14 at 19:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

@milo might have a point. WordPress loads jQuery in no-conflict mode.

So this:

$(document).ready(function(){
    alert('ready');
});

becomes this:

jQuery(document).ready(function(){
    alert('ready');
});

if you want to use $ to not have to refactor your code just pass it to the function:

jQuery(document).ready(function($){
    $('.selector').html('this will now work');
});

Not the answer you're looking for? Browse other questions tagged or ask your own question.