Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Onclick function "is not defined"

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?

like image 219
Atrag Avatar asked Jun 27 '16 10:06

Atrag


1 Answers

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?

like image 196
Lilamuse Avatar answered Oct 24 '22 04:10

Lilamuse