EylonS
EylonS

Reputation: 29

I'm tring to create very simple wordpress plugin but my javascript not working

I had already connected the javascript file

Enqueue script function is:

function wpplugin_admin_scripts(){
   wp_enqueue_script(
    'myplugin-admin',
    WPPLUGIN_URL.'admin/js/eylonplugin-admin.js',
    ['jquery'],
    time()
   );

 }
 add_action('admin_enqueue_scripts', 'wpplugin_admin_scripts', 100);

But while I'm trying to style some attributes I can't.

This is the function that I'm adding HTML to my plugin page :

 function my_plugin_plugin_html(){ ?>
    
    <div id="wrap container">
        <h3> Plugin Header </h3>
    
        <form method="post" action='options.php'>
            <?php settings_errors(); ?>
            <?php settings_fields('my_plugin_option_group'); ?>
            <label for="my_plugin_field_plugin">Top bar color</label>
            <input name="my_plugin_field" id="my_plugin_field_plugin" type="text" value="<?php echo get_option('my_plugin_field')?>"><br>        
            <div>
            <?php submit_button('my_plugin I love you');?> 
        </form>
        <h2 id="js_checking">my_plugin<span class="selected_output"></span></h2>
        <script> </script>
        </div>
        <h3>My color is red</h3>
        <button id="try_me">Try me!</button>
    <?php
}

Here I'm trying to style my h3 attribute but nothing happens. But the alert is working.

$("h3").css("color","red");
alert("Hello world");

Upvotes: 1

Views: 51

Answers (1)

Foysal imran
Foysal imran

Reputation: 133

Wrap your jquery code with as below so you can add codes without using jQuery instead of $:

(function ($) {

YOUR CODES HERE

})(jQuery);

Upvotes: 1

Related Questions