Title: Copying working javascript code to wordpress
Last modified: September 11, 2017

---

# Copying working javascript code to wordpress

 *  [mateox](https://wordpress.org/support/users/mateox/)
 * (@mateox)
 * [8 years, 6 months ago](https://wordpress.org/support/topic/copying-working-javascript-code-to-wordpress/)
 * Hey guys! This is my first post so don’t be angry about any mistakes i make 🙂
 * I found some working javascript code at this page [Link](http://jsfiddle.net/Lwxoeyyp/1/)
   [http://jsfiddle.net/Lwxoeyyp/1/](http://jsfiddle.net/Lwxoeyyp/1/) and I want
   to copy it to my wordpress site so it will work.
 * I already created a custom javascript file, uploaded it to my webspace and checked
   if it is working (it is). Now the question is, what do I have to change to make
   it work? At the moment, the my custom “jscript.js” file is looking like this:
 *     ```
       function save(){
           var checkbox = document.getElementById('checkbox1zaal1');
           localStorage.setItem('checkbox1zaal1', checkbox.checked);
       }
   
       function load(){    
           var checked = JSON.parse(localStorage.getItem('checkbox1zaal1'));
           document.getElementById("checkbox1zaal1").checked = checked;
       }
   
       function wis(){
           location.reload();
           localStorage.clear()
   
       }
   
       load();
       ```
   
 * Any tips? Thank you very much!
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fcopying-working-javascript-code-to-wordpress%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

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

 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [8 years, 6 months ago](https://wordpress.org/support/topic/copying-working-javascript-code-to-wordpress/#post-9483916)
 * Use wp_enqueue_script to load your script in the header or footer
 * [https://developer.wordpress.org/reference/functions/wp_enqueue_script/](https://developer.wordpress.org/reference/functions/wp_enqueue_script/)
 *  [Andrew Nevins](https://wordpress.org/support/users/anevins/)
 * (@anevins)
 * WCLDN 2018 Contributor | Volunteer support
 * [8 years, 6 months ago](https://wordpress.org/support/topic/copying-working-javascript-code-to-wordpress/#post-9485733)
 * If you’re not using a custom theme then consider setting up a Child Theme before
   enqueing the script: [https://codex.wordpress.org/Child_Themes](https://codex.wordpress.org/Child_Themes)
 *  Thread Starter [mateox](https://wordpress.org/support/users/mateox/)
 * (@mateox)
 * [8 years, 6 months ago](https://wordpress.org/support/topic/copying-working-javascript-code-to-wordpress/#post-9486664)
 * I used wp_enqueue_script to load it in the header:
 * The code in my functions.php is
 *     ```
       function cool_scripts() {
       wp_enqueue_script( 'cool-stuff', get_stylesheet_directory_uri() . '/custom_js/scripts.js', array('jquery'), 'false', false);
       }
       add_action( 'wp_enqueue_scripts', 'cool_scripts' )
       ```
   
 * My current scropts.js looks like this:
 *     ```
       function save(){
           var checkbox = document.getElementById('checkbox1zaal1');
           localStorage.setItem('checkbox1zaal1', checkbox.checked);
       }
   
       function load(){    
           var checked = JSON.parse(localStorage.getItem('checkbox1zaal1'));
           document.getElementById("checkbox1zaal1").checked = checked;
       }
   
       function wis(){
           location.reload();
           localStorage.clear()
   
       }
   
       load();
       ```
   
 * I think I have to change the code in order to make it work. Do you have any idea
   why this doesn’t work?
 * Best regards
 * Mathias
 *  Moderator [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/)
 * (@sterndata)
 * Volunteer Forum Moderator
 * [8 years, 6 months ago](https://wordpress.org/support/topic/copying-working-javascript-code-to-wordpress/#post-9486688)
 * Does the link to the script appear in the header?
 *  Thread Starter [mateox](https://wordpress.org/support/users/mateox/)
 * (@mateox)
 * [8 years, 6 months ago](https://wordpress.org/support/topic/copying-working-javascript-code-to-wordpress/#post-9486716)
 * Yes it does. The last function “wis” also seems to work. You can test it on [http://www.sprts.de](http://www.sprts.de)

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

The topic ‘Copying working javascript code to wordpress’ is closed to new replies.

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 5 replies
 * 3 participants
 * Last reply from: [mateox](https://wordpress.org/support/users/mateox/)
 * Last activity: [8 years, 6 months ago](https://wordpress.org/support/topic/copying-working-javascript-code-to-wordpress/#post-9486716)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
