0

I would like to customize some form elements in my WP admin area: starting from this example (http://api.jquery.com/val/ the one with the select) what I would liko to do, is to update a via a select field. In order to do so I started including this function:

function my_enqueue() {

    wp_enqueue_script( 'my_custom_script', get_template_directory_uri() . '/js/admin.js' );
}
add_action( 'admin_enqueue_scripts', 'my_enqueue' );

Inside "admin.js" I placed:

alert("It works!");

And it does work. But whenever I try to include anything related to jQuery nothing happens: the code is being included inside the page and I get no errors in Firebug. For example a script as simple as the one you can find at http://www.w3schools.com/jquery/event_keyup.asp

$("input").keyup(function(){
    $("input").css("background-color","pink");
}); 

Doesn't work. Nothing happens. So I wonder, what am i missing or doing wrong? Thansk a lot!

  • Do you have the code wrapped in document ready wrappers? Have you made sure to support "$"? What is the full code of admin.js? – Jake Sep 16 '13 at 16:54
0

I am not sure this is your problem if you are not getting any errors, but make sure you have your jQuery code within document ready wrappers and pass the $ in to make sure you can access it since Wordpress runs in no-conflict mode.

jQuery(document).ready(function($) {

    $("input").keyup(function(){
        $("input").css("background-color","pink");
    });

});

It would also be good to declare jQuery as a dependency just to be safe.

wp_enqueue_script( 'my_custom_script', get_template_directory_uri() . '/js/admin.js', array( 'jquery' ) );
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.