Jquery for metaboxes plugin

Im using jQuery for other elements on my admin page where I edit pages but with this plugin it refuses to even cast a simple alert

Jquery:

Read More
(function ($) {
$(document).ready(function () {
$( "#target" ).click(function() {
alert( "Handler for .click() called." );
});
});
});

HTML

<div id="target">
Click here
</div>

I even tried to load jquery in my document:

wp_enqueue_script('jquery');

To no avail. If I only run regular javascript it works as intended:

alert("test");

gives the alert as it should.

Related posts

1 comment

  1. Your Javascript is not working because you are not calling the anonymous function. A better way to write your code would be:

    jQuery(document).ready(function( $ ) {
         $( "#target" ).click(function() {
           alert( "Handler for .click() called." );
        });
    });
    

    This calls the jQuery(document).ready() function, which executes the enclosed anonymous function when the DOM has fully loaded.

    Helpful links:

    Using jQuery in WordPress

    Self-Executing Anonymous Functions or How to Write Clean Javascript

Comments are closed.