Reputation: 29
SO, It has been a few hours since I enqueued one custom script and one style sheet to my WordPress website functions. Enqueuing, those two worked perfectly; however, Chrome and Mozilla developer tools threw syntax error for my script. Below is the script.
<script>
var coll = document.getElementsByClassName("collapsible");
var i;
for (i = 0; i < coll.length; i++) {
coll[i].addEventListener("click", function() {
this.classList.toggle("active");
var content = this.nextElementSibling;
if (content.style.display === "block") {
content.style.display = "none";
} else {
content.style.display = "block";
}
});
}
</script>
Here is how I enqueued the script to my WordPress website functions.
function enqueue_my_custom_script() {
wp_enqueue_script( 'my-custom-js', 'path-to-the-file', false );
}
add_action( 'wp_enqueue_scripts', 'enqueue_my_custom_script' );
And lastly, here is the syntax error that Google and Mozilla Developer tools throw.
(index):160 Uncaught SyntaxError: Unexpected token '<'
Now, I have a series of questions.
<script></script>
tags in a JS file for wordpress?Upvotes: 2
Views: 323
Reputation: 1533
You don't include <script></script>
tags in JavaScript files. <script>
is a HTML tag, your JavaScript file is not a HTML file.
Upvotes: 0
Reputation: 562
<script></script>
tags to either include .js file or write js code between the tags. In the last option, you specify that this is javascript code, and should be handled as one.Upvotes: 1