• Resolved nutsmuggler

    (@nutsmuggler)


    Hello folks,
    I am refining a plugin for public distribution. Presently I am trying to implement client-side validation through jQuery, but for some reason I cannot load jQuery itself.
    This part of the plugin is in a custom admin page. I tried some simple jQuery code; to be on the safe side, I used a no-conflict function.

    function test_js() {
    wp_enqueue_script('jquery');
    ?>
    <script type="text/javascript">
    	$j=jQuery.noConflict();
    	$j(document).ready(function() {
       $j("div.debug").hide();
     });
    </script>
    <?php
    }
    add_action('admin_print_scripts','test_js');

    Firebug returns “jQuery is not defined”. Yet jQuery is correctly loaded, as the HTML code shows:

    <script type='text/javascript' src='http://pattybook.local/~davidebenini/lucianacorsi/wp-includes/js/jquery/jquery.js?ver=1.2.3'></script>
    <script type='text/javascript' src='http://pattybook.local/~davidebenini/lucianacorsi/wp-admin/js/common.js?ver=20080318'></script>
    <script type='text/javascript' src='http://pattybook.local/~davidebenini/lucianacorsi/wp-includes/js/jquery/jquery.color.js?ver=2.0-4561'></script>

    This code gets inserted after my script’s code, is that a problem?
    Anyway, jQuery doesn’t work even from the Firebug console; eveything seems to indicate that it is not loaded, but it is.
    Any suggestion?
    Davide

Viewing 6 replies - 1 through 6 (of 6 total)
  • Actually you can just use jQuery(“#test”)
    <script type="text/javascript">
    jQuery(document).ready(function() {
    jQuery("div.debug").hide();
    });
    </script>

    Thread Starter nutsmuggler

    (@nutsmuggler)

    Thanks for the info. Anyway, jQuery(“#test”) produces the same result: “jQuery is not defined”. Any suggestion?
    Cheers,
    Davide

    I don’t think you need to enqueue/print jquery out as by default jQuery is already loaded when you are in wp-admin

    Thread Starter nutsmuggler

    (@nutsmuggler)

    Yes, I added the enque call subsequently, because of the error. Anyway, without the enque call the behaviour is the same: the script is loaded in the html head, but I get the “jQuery is not defined error.
    Thanks for the suggestion anyway 🙂
    Any other idea?
    Cheers,
    Davide

    Thread Starter nutsmuggler

    (@nutsmuggler)

    Ok, I solved the issue.
    The problem was the hook; “admin_print_scripts”, the Codex suggestion for embedding Javascript, is actually called AFTER the jQuery inclusion. I had to use “admin_head”. Someone should specify this potential problem in the codex.
    Thanks for the brainstorming,
    Davide

    nutsmuggler, I’m having the same issue with a plugin I installed can you please help me out to figure out what’s wrong?
    http://tesis.ideartte.com

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Unable to use jquery in my plugin’ is closed to new replies.