Failing to make Javascript work with WordPress :'(

[ad_1]

Hey guys, hate having to ask help for this one but I’ve been trying to add Javascript enqueue for a few days now but can’t get it to work. I tried different things but this is my latest attempt after re-installing the Child theme and pasting the code in the functions.php file.

​

Also tried adding this (and different types of code) in footer/header with WPCode plugin but nothing happens.

This is what I’m trying to test:

*(mainly trying to figure out how to make JS work at all)*

**HTML:**

<button id=”slide-button”>Click to slide up</button>
<div id=”slide-element”>Slide me up</div>

**JS:**

const button = document.querySelector(“#slide-button”);

const element = document.querySelector(“#slide-element”);

button.addEventListener(“click”, function() {

if (element.style.height === “100px”) {

element.style.height = “0px”;

} else {

element.style.height = “100px”;

}

});

**CSS:**

\#slide-element {
height: 100px;
transition: height 0.5s ease-out;
overflow: hidden;
}

&#x200B;

Thank you in advance!

[ad_2]
6 Comments
  1. Okok hold on, I’m trying:
    add_action( ‘wp_enqueue_scripts’, ‘add_slidebuttonjs’ );
    function add_slidebuttonjs() {
    wp_enqueue_script( ‘slidebutton.js’, get_stylesheet_directory_uri() . ‘/slidebutton.js’, false, ‘1.0’, true );
    }

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer