Title: Load javascript in wp-admin/widgets.php
Last modified: August 24, 2016

---

# Load javascript in wp-admin/widgets.php

 *  [rauman](https://wordpress.org/support/users/rauman/)
 * (@rauman)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/load-javascript-in-wp-adminwidgetsphp/)
 * Hello,
    I have a question for advanced developers.
 * I’m struggling to understand how I can load a javascript file for a specific 
   widget that I’m working on.
 * Within the class of the widget I have the `public function form( $instance ) {}`
   that contains the fields of the widget.
    I have two selectors and I want to show
   the second selector based on the first selector. I’ve tested some simple selectors
   in my theme (with the js included and jquery) and it works. But it seems that
   adding this lines
 * > wp_register_script( ‘widget’, get_template_directory_uri() . ‘js/widget.js’,
   > array(‘jquery’), ‘null’ );
   >  wp_enqueue_script(‘widget’);
 * in
 * > public function form( $instance ) {}
 *  doesn’t load the js files properly, and the “selector thing” doesn’t work.
 * I tried registering and enqueueing widget.js in functions.php, but obviously 
   it doesn’t work because the selectors are in backend not frontend of wordpress.
 * My questions is not about how to show the second selector based on the first 
   selector.
 * My question is how to load javascript in `public function form( $instance ) {}`?
   
   What is the best way to load custom javascript for a widget in Dashboard > Widgets?
 * Thank you!

Viewing 1 replies (of 1 total)

 *  [Ryan](https://wordpress.org/support/users/ryanmclaughlin1/)
 * (@ryanmclaughlin1)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/load-javascript-in-wp-adminwidgetsphp/#post-6095740)
 * hello rauman – the form method is used to render the widget and by that point
   it is too late to enqueue scripts.
 * try creating a separate method that gets hooked to admin_enqueue_scripts ([https://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts](https://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts))
   and then register and enqueue your script from there.

Viewing 1 replies (of 1 total)

The topic ‘Load javascript in wp-admin/widgets.php’ is closed to new replies.

## Tags

 * [javascript](https://wordpress.org/support/topic-tag/javascript/)
 * [widget](https://wordpress.org/support/topic-tag/widget/)

 * In: [Hacks](https://wordpress.org/support/forum/plugins-and-hacks/hacks/)
 * 1 reply
 * 2 participants
 * Last reply from: [Ryan](https://wordpress.org/support/users/ryanmclaughlin1/)
 * Last activity: [11 years, 1 month ago](https://wordpress.org/support/topic/load-javascript-in-wp-adminwidgetsphp/#post-6095740)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
