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.
- Why the script is not removed when I removed the queueing code from WordPress functions? I still see the script on page source code even after removing the function. (I see the JS file is not embedded in my website, but I see the actual script on my homepage. )
- Is it wrong to include
<script></script>tags in a JS file for wordpress? - The Google developer and Mozilla both indicates the code is on index.php, but I couldn't find it anywhere on my theme as it has multiple homepages templates. Where could this file be located?
- the fourth one and the most important of all, how can I get rid of this error and the script?