Skip to content
Advertisement

Creating an extension js inclusion problem

I am creating a small plugin with JS animation. In my plugin, I created an index.php file for my shortcode, functions.php to call my JS code and a JS file for the animation. Anyway my shortcode works, but not the animation

So I called all these files in my console, it seems to tell me that I don’t have an error. But when I want to make my JS work it doesn’t work.

Here is my code.

Index.php

require plugin_dir_path(__FILE__) . 'functions.php';
function my_function($atts){
    echo '<button class="test" onclick="myTest">'. $title_button_clip .'</button>';
    echo '<p id="demo"></p>';
}
add_shortcode('short', 'my_function');

functions.php

function myfunction_two(){
    wp_enqueue_script ('script_clipboard', plugin_dir_url(__FILE__).'js/script.js', 1.0, true);
}
add_action('wp_enqueue_scripts', 'myfunction_two');

script.js

function myTest(){
    document.getElementById("demo").innerHTML = "hello";
}

Advertisement

Answer

The Only problem I have seen is just the way you are invoking the Javascript function name.

This is not the correct way for inline invoking

<button class="test" onclick="myTest">'. $title_button_clip .'</button>

It should be like this

<button class="test" onclick="myTest()">'. $title_button_clip .'</button>

I just added parenthesis to the myTest function name

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement