Adding jquery to wordpress site -


i new working jquery, sorry if basic stuff.

i have been playing around on jsfiddle , got jquery want (add class parent when hover on child), when take code , try add wordpress site, not work.

this code trying add:

$('.explore > .country').hover(function() { $(this).parent().toggleclass('hover'); }) 

i know wordpress doesn't $ function , tried this:

<script type="text/javascript"> (function($) { $('.explore > .country').hover(function() { $(this).parent().toggleclass('hover'); }) })(jquery); </script> 

here link jsfiddle

any appreciated.

be sure enqueue jquery first. comes built in wordpress doesn't come pre-activated.

search theme's function.php file "wp_enqueue", if there function there enqueuing scripts add following line it..

wp_enqueue_script( 'jquery' ); 

otherwise, create own enqueue scripts function..

// function enabled (enqueue) scripts in wordpress function enqueue_scripts() {     wp_enqueue_script('jquery'); } add_action('wp_enqueue_scripts','enqueue_scripts'); 

update


alternatively

you can create js file custom code , enqueue use site wide.

  1. create file named mycustomfunctions.js

  2. put in it..

    // describe function here jquery(function() {     jquery('.explore > .country').hover(function() {         jquery(this).parent().toggleclass('hover');     })     jquery('.explore > .community').hover(function() {         jquery(this).parent().toggleclass('hover2');     }) }); 
  3. in child theme root folder create new folder, name 'js'

  4. put mycustomfunctions.js in js folder

  5. edit child themes functions.php include..

    // function enable (enqueue) scripts in wordpress function enqueue_scripts() {     wp_enqueue_script('mycustomfunctions', get_stylesheet_directory_uri() . '/js/mycustomfunctions.js', array('jquery') ); } add_action('wp_enqueue_scripts','enqueue_scripts'); 

note - no need enqueue jquery separately we've listed dependency mycustomfunctions (see array), wordpress enqueue jquery automatically because of required dependency.


Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

python 3.5 - Pyqtgraph string in x tick -