I am fairly new to wordpress and am trying to define and call a function but cannot get it to work.
The following code appears in a php file that is called from the content.php file using get_template_part.
<div onclick="checkAllTopicCheckBoxes()"> </div>
I added the following code to the bottom of the function.php file:
function checkAllTopicCheckBoxes() {
alert("Hello! I am an alert box!!");
}
When I click the element it returns:
(index):363 Uncaught ReferenceError: checkAllTopicCheckBoxes is not defined.
Can anyone please help me?
What you wrote is a Javascript function, it must be surrounded by in HTML code. Have you written your function in something like:
echo('<script>function checkAllTopicCheckBoxes() { alert("Hello! I am an alert box!!");}</script>');
Also, I think Wordpress must have some function ready to use to add scripts. Try to have a look on wp_enqueue_script (string $handle, string $src = false, array $deps = array(), string|bool|null $ver = false, bool $in_footer = false);
edit: I just found that question, it may answer yours: How to write a Javascript function inside the functions.php of Wordpress?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With