Reputation: 57
I have a generic javascript function which should toggle a class on and off, however I cannot call the funtion.
I can see the function getting loaded on the network tab of inspect element, I can even read my script, but even in the console tab, I cannot call my function. I have done this many times before however cannot figure this one out.
My script is as follows:
function updateTags(tagSelected){
if(document.getElementById(tagSelected).classList.contains("selected")){
document.getElementById(tagSelected).classList.remove("selected");
} else{
document.getElementById(tagSelected).classList.add("selected");
}
}
button{
background-color:green;
}
.selected{
background-color:red !important;
}
<button class="tag-button" onclick="updateTags('SUP')" id="SUP">SUP</button><br>
The only thing to note is that the buttons that call the script are loaded by a php script seperate to the main page. They are loaded in using a require, and all appear correctly. If I copy and paste the function into the console tab, it becomes usable, so I just cannot understand what is happening!
Below are screenshots of my script being loaded, and the error I am getting.
Upvotes: 0
Views: 75
Reputation: 57
Nevermind, just spent the last 2 hours trying to figure this out. Turns out, I was missing a plus sign in the second function in my script. Fixed now, and works perfectly, thanks!
Upvotes: 1