jQuery script is not working inside 'head' tag
-
so i wrote a simple script and enqueued it properly.
my-jquery-script.js:
jQuery(document).ready(function($) { $('#some_id').on('click','.clone',function(e){ alert('clicked'); /*.....*/ e.preventDefault(); } });
functions.php:
add_action( 'wp_enqueue_scripts', 'my_function' ); function my_function() { wp_enqueue_script( 'my-jquery-script', get_stylesheet_directory_uri() . '/js/my-jquery-script.js', array('jquery') ); }
some-page.php:
<?php wp_head(); ?> <body> <form> <div id="some_id"> <a href="#" class="clone">button</a> </div> </form> </body> <?php wp_footer(); ?>
it’s not working but when i include jQuery script manually into some-page.php like this:
<?php wp_head(); ?> <body> <form> <div id="some_id"> <a href="#" class="clone">button</a> </div> <script type='text/javascript' src='<?php echo get_stylesheet_directory_uri() . "/js/my-jquery-script.js"; ?>'></script> </form> </body> <?php wp_footer(); ?>
it’s working but “$(document).ready () fires twice” as mentioned here: http://stackoverflow.com/questions/10727002/jquery-document-ready-fires-twice
What am i doing wrong? Any help would be appreciated.
update: I also noticed that $(document.body).on(‘click’,’.clone’,function(e){ } selector is working instead of id selector.
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘jQuery script is not working inside 'head' tag’ is closed to new replies.