Replies: 0
Hi
I’m trying to learn how to use javascript in wordpress and doing this by doing some really simple JS-functions. However have run into a problem when placing the script in an external file.
(not sure if this is the correct forum, but starting here)
in my external file, called myScript.js I have this code:
function myFunction() {
document.getElementById(‘demo’).innerHTML = “Paragraph changed.”;
}
in functions php
function your_scripts() {
wp_register_script(‘myFunction’, ‘/js/myScript.js’, false);
// This registers your script with a name so you can call it to enqueue it
wp_enqueue_script( ‘myFunction’ );
// enqueuing your script ensures it will be inserted in the propoer place in the header section
}
add_action(‘wp_enqueue_scripts’, ‘myFunction’);
And in my header-php
<p id=”demo”>A Paragraph.</p>
<button type=”button” onclick=”myFunction()”>Try it</button>
So the myScript.js seems to be loading but when clickin the button I get the error myFunction not defined
cannot understand what I’ve done wrong..