Title: Can&#039;t Run JavaScript from Custom Widget
Last modified: August 19, 2016

---

# Can't Run JavaScript from Custom Widget

 *  [Lyden](https://wordpress.org/support/users/lyden/)
 * (@lyden)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/cant-run-javascript-from-custom-widget/)
 * Hey,
 * New to WordPress. I’m trying to create a dynamic widget options form in the Admin
   Panel for my custom widget. I can’t seem to modify any DOM elements of this form
   from an external JavaScript file. I am able to run alert(“hello from JavaScript”)
   though, so I know I am successfully loading the script via wp_enqueue_script().
   I have also tried running the script from a test HTML script, and when invoked
   from my test page, the script functions as it should.
 * The Script (monthAngel.js) :
 *     ```
       function addAngelForm() {
       	//alert("hello from javascript");
       	var form = document.getElementById("addAngelButton").parentNode;
       	var paragraph = document.createElement("p");
       	var text = document.createTextNode("test text for new paragraph");
       	paragraph.appendChild(text);
       	form.appendChild(paragraph);
       }
       ```
   
 * Enqueue Script (in functions.php) :
 *     ```
       function add_header_scripts() {
       		wp_enqueue_script('monthAngelScript',
       			get_bloginfo('template_directory') . '/js/monthAngel.js');
       	}
   
       add_action('admin_print_scripts', 'add_header_scripts');
       ```
   
 * In WP_Widget Subclass (declared in functions.php) :
 *     ```
       function form($instance) {
       			?>
       			<p>
       				<label for="<?php echo $this->get_field_id('title');?>">Title: </label>
       				<input id="<?php echo $this->get_field_id('title');?>"
       					name="<?php echo $this->get_field_name('title');?>"
       					type="text"
       					value="<?php  echo esc_attr($instance['title']);?>" />
       				<input class="button-primary" style="margin:5px; float:right;"
       					id="addAngelButton" type="button" onClick="addAngelForm()"
       					value="Add Angel"/>
       			</p>
       			<?php
       		}
       ```
   
 * Any ideas?

Viewing 3 replies - 1 through 3 (of 3 total)

 *  Thread Starter [Lyden](https://wordpress.org/support/users/lyden/)
 * (@lyden)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/cant-run-javascript-from-custom-widget/#post-1901789)
 * anyone?
 *  Thread Starter [Lyden](https://wordpress.org/support/users/lyden/)
 * (@lyden)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/cant-run-javascript-from-custom-widget/#post-1901803)
 * in the post above, it should say: “from a test HTML page” not: “from a test HTML
   script.” Sorry for confusion
 *  Thread Starter [Lyden](https://wordpress.org/support/users/lyden/)
 * (@lyden)
 * [15 years, 3 months ago](https://wordpress.org/support/topic/cant-run-javascript-from-custom-widget/#post-1901852)
 * anyone?

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Can't Run JavaScript from Custom Widget’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 3 replies
 * 1 participant
 * Last reply from: [Lyden](https://wordpress.org/support/users/lyden/)
 * Last activity: [15 years, 3 months ago](https://wordpress.org/support/topic/cant-run-javascript-from-custom-widget/#post-1901852)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
