K4S3
K4S3

Reputation: 163

jQuery code not working with wordpress site

So im working on a site, a custom wp theme with a _s boilerplate, i've enqueue my styles and scripts which the browser picks up fine no errors;

wp_enqueue_style( 'bootstrap-css', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css' );

    wp_enqueue_script( 'bootstrap-js', 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js', array(jquery), true);

    wp_enqueue_style( 'cbd-yums-style', get_stylesheet_uri() );

    wp_enqueue_script( 'cbd-yums-scripts', get_template_directory_uri(). '/js/scripts.js', array(), true ); 

scripts.js is where my jquery code is

scripts.js is where my jquery code is AND IM TRYING TO CHANGE THE TEXT COLOR OF MY MAIN NAVIGATION ITEMS i tried this code believing it would work:

( function( $ ) {

    $('.primary-menu .menu-item').css('background-color', '#AED038');

} ) ( jQuery );

& the corresponding HTML

<nav id="site-navigation" class="main-navigation col-md-6 col-lg-6">
  <button class="menu-toggle" aria-controls="primary-menu" aria-expanded="false">Primary Menu</button>
  <div class="menu-header-menu-container">
    <ul id="primary-menu" class="menu nav-menu" aria-expanded="false">
      <li id="menu-item-44" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-44"><a href="#home">Home</a></li>
      <li id="menu-item-45" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-45"><a href="#about">About</a></li>
      <li id="menu-item-46" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-46"><a href="#products">CBD Products</a></li>
    </ul>
  </div>        
</nav>

But no change on browser reload, so i went back to w3schools to check my syntax. i modified the jquery css() example; and it seems my syntax is fine

Can anybody see that i'm doing wrong?

Upvotes: 1

Views: 1021

Answers (3)

Dani&#235;l Visser
Dani&#235;l Visser

Reputation: 581

A page can't be manipulated safely until the document is "ready." - http://learn.jquery.com/using-jquery-core/document-ready/

Alter your code like this:

(function($) {

    $(document).ready(function() {

        $('#primary-menu .menu-item').css('background-color', '#AED038');

    });

})(jQuery);

Now the code will execute once the document is ready.

And as Hafeez Hamza pointed out: change .primary-menu to #primary-menu.

Upvotes: -1

K4S3
K4S3

Reputation: 163

I FOUND THE PROBLEM!! 1. i didn't have jQuery properly loaded as a dependency 2. I didn't make sure jQuery was written in not conflict mode

so i simply changed it to

 jQuery(document).ready(function($){
    $('#primary-menu .menu-item').css('background-color', '#AED038');
});

and in the functions.php where i have to enqueue scripts and styles

wp_enqueue_script( 'cbd-yums-scripts', get_template_directory_uri(). '/js/scripts.js', array( 'jquery' ), '20181025' ,true ); 

thank you everyone for helping me narrow down the problem

Upvotes: 0

Hafeez Hamza
Hafeez Hamza

Reputation: 814

you have to select ids with # prefix in jquery, in current situation call in id with . prefix that means class name, jquery is searching class name primary-menu, since you dont have any element with class name primary-menu, the style is ignored.

the issue can be solved by change the query $('#primary-menu .menu-item').css('background-color', '#AED038');

Upvotes: 3

Related Questions