• 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 Linkhttps://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 to see the link]

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Use wp_enqueue_script to load your script in the header or footer

    https://developer.www.ads-software.com/reference/functions/wp_enqueue_script/

    Andrew Nevins

    (@anevins)

    WCLDN 2018 Contributor | Volunteer support

    If you’re not using a custom theme then consider setting up a Child Theme before enqueing the script: https://codex.www.ads-software.com/Child_Themes

    Thread Starter mateox

    (@mateox)

    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)

    (@sterndata)

    Volunteer Forum Moderator

    Does the link to the script appear in the header?

    Thread Starter mateox

    (@mateox)

    Yes it does. The last function “wis” also seems to work. You can test it on https://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.