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
JavaScript
x
7
1
require plugin_dir_path(__FILE__) . 'functions.php';
2
function my_function($atts){
3
echo '<button class="test" onclick="myTest">'. $title_button_clip .'</button>';
4
echo '<p id="demo"></p>';
5
}
6
add_shortcode('short', 'my_function');
7
functions.php
JavaScript
1
5
1
function myfunction_two(){
2
wp_enqueue_script ('script_clipboard', plugin_dir_url(__FILE__).'js/script.js', 1.0, true);
3
}
4
add_action('wp_enqueue_scripts', 'myfunction_two');
5
script.js
JavaScript
1
4
1
function myTest(){
2
document.getElementById("demo").innerHTML = "hello";
3
}
4
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
JavaScript
1
2
1
<button class="test" onclick="myTest">'. $title_button_clip .'</button>
2
It should be like this
JavaScript
1
2
1
<button class="test" onclick="myTest()">'. $title_button_clip .'</button>
2
I just added parenthesis to the myTest
function name