Support » Fixing WordPress » jQuery is not defined when using admin_enqueue_scripts

  • Resolved giwrgos88

    (@giwrgos88)



    Hello everyone, I’m working on a plugin and I’m looking for a help about an issue that I have. Basically I have created a function inside the index file of my plugin which contains a javascript code. This function i want to use it only in a specific page inside the admin page that why i use the admin_enqueue_scripts.

    add_action('admin_enqueue_scripts','my_javascript_code')

    The problem that I have is that on the console page when I load the page shows the following message “Uncaught ReferenceError: jQuery is not defined” but the code is working fine.

    Here is my code

    function menu_options()
    {
        echo '<script type="text/javascript">
    jQuery(document).ready(function() {
        jQuery(".level2 li").hover(function () {
    
            jQuery(this).append("<li class="menu_options">hello</li>")
        }, function () {
    
            jQuery(this).children(".menu_options").remove();
        })
        });
        });
    </script>';
    }

    What i’m doing wrong? Also how i can call this function only in a specific page?

    Thank you

Viewing 1 replies (of 1 total)
  • Ok i found the solution. I have created an js file where i copy/paste the code there and removed the <script> tags. then using the following code i’m calling the ajax file

    function menu_options() {
        wp_enqueue_script('menu_option_ajaxscript', AJAXACTIONS.'/il_menu/il_menu_admin/ajax/menu_ajax.js', array('jquery'));
        wp_localize_script( 'menu_option_ajaxscript', 'menu_option_ajaxscript', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ) ) );
    }
    
    add_action('admin_enqueue_scripts', menu_options);
Viewing 1 replies (of 1 total)
  • The topic ‘jQuery is not defined when using admin_enqueue_scripts’ is closed to new replies.