Chris
Chris

Reputation: 767

Getting jQuery to work in new build WordPress theme

I am new to jQuery and have tried to get scripts to work to no avail. I would like to simplify my script to just see that my theme is in fact recognizing my scriptfile.js

My js file is just:

var success = 'SUCCESS!';

  jQuery('nav .home a').hover(function()
  {
    echo(success);
  });

my functions.php file has this:

<?php
function add_my_script() {
wp_enqueue_script(
'preview',
 get_template_directory_uri() . '/js/scriptfile.js', 
array('jquery') 
);
}
add_action( 'wp_enqueue_scripts', 'add_my_script' );
?>

My header.php contains:

<script type="text/javascript"><!--//--><![CDATA[//><!--
jQuery(document).ready(function(jQuery){

});
 //--><!]]></script>

I am not getting any errors in firebug, but no output either.

Upvotes: 0

Views: 70

Answers (2)

Satyajit
Satyajit

Reputation: 3859

I believe what's happening is that your script file is being included fine but the way it's written the hover event attachment will happen before the DOM is ready. Try this, simply say alert(success); in your js file and see if the alert pops up. If it does, then wrap the hover event handler in a function in your script file and call that function from the document.ready function handler in your header.php. By the way, what's the echo function? Probably you are looking for alert.

Upvotes: 1

Spokey
Spokey

Reputation: 10994

Not sure if this is the problem but first of all your selector is not exactly right.

var success = 'SUCCESS!';

jQuery('nav .home a').hover(function(){ ... });

nav should be .nav for class or #nav for id

echo(success);

Unless echo() is a function somewhere it will not work. If you want to show an alert message use alert(success)

Upvotes: 4

Related Questions