I have the following code executing from a click event listener function
if (target.tagName === 'LABEL'){
if (target.classList.contains('active')){
console.log('removing')
target.classList.remove('active')
}
else {
console.log('adding')
console.log(target.classList)
target.classList.add("active");
console.log(target.classList)
}
}
In both Chrome and Firefox, The issue is that it's not actually adding the class and I can't figure out why.
This same code works elsewhere, but in this one particular section it's not working.
Here is what I get from the console
click registered
LABEL
adding
DOMTokenList(3)
0: "topic-label"
1: "btn"
2: "initialized"
length: 3
value: "topic-label btn initialized"
<prototype>: DOMTokenListPrototype { item: item(), contains: contains(), add: add(), … }
DOMTokenList(4)
0: "topic-label"
1: "btn"
2: "initialized"
length: 3
value: "topic-label btn initialized"
<prototype>: DOMTokenListPrototype { item: item(), contains: contains(), add: add(), … }
minimizing the DOMTokenList I see the following
click registered
LABEL
adding
DOMTokenList(3) [ "topic-label", "btn", "initialized" ]
DOMTokenList(4) [ "topic-label", "btn", "initialized", "active" ]
I can see that I am properly getting to that area of my code, and I can see that the DomTokenList changes - but the actual classList is not changing.
I don't know how to even begin googling this, so maybe someone knows what's happening?
Thanks in advance. RR
More details
Here is the full event listener method
function initialize_topics(){
var all_topics = byClass('topic-label')
for (var i=0; i< all_topics.length; i++){
if (all_topics[i].classList.contains('initialized')){
continue
}
else{
all_topics[i].classList.add('initialized');
all_topics[i].addEventListener("click", function(e){
//console.log('click registered')
e=e || window.event;
var target = e.target || e.srcElement;
//console.log(target.tagName)
if (target.tagName === "LABEL"){
/*
if (target.classList.contains('active')){
console.log('removing')
target.classList.remove('active')
}
else {
console.log('adding')
console.log(target.classList)
target.classList.add("active");
console.log(target.classList)
}
*/
console.log(target.firstElementChild.value)
console.log(target.classList)
target.classList.toggle('active')
console.log(target.classList)
}
});
}
}
}
This is the HTML of what I'm targeting here
<div class="lurnby_data_group topics-group" data-toggle="buttons">
<h6 class="inline-block">Topics:</h6>
<span id="CreateTopic">
<button onclick ="NewTopic()" class="main-button save">
Create new topic
</button>
</span><br>
<div class = "topics-all">
{% for topic in topics %}
<label class="topic-label btn">
<input name = "topics" type="checkbox" value="{{topic.title}}">
{{topic.title}}
</label>
{% endfor %}
<span id = "topicplaceholder"></span>
</div>
</div>
It's also not working in Chrome it seems. Here's a small vid where you can see what's happening a bit more.
https://www.loom.com/share/12703437f9a04d4e9ef44d3c933353de
And here is what I'm getting in the console now. I am first logging the value of the input, then the classlist, then toggling 'active', then logging the classlist again.
The issue is that the change is not actually being pushed to the DOM.
test
DOMTokenList(3) [ "topic-label", "btn", "initialized" ]
DOMTokenList(4) [ "topic-label", "btn", "initialized", "active" ]
hellop
DOMTokenList(3) [ "topic-label", "btn", "initialized" ]
DOMTokenList(4) [ "topic-label", "btn", "initialized", "active" ]
And all the elements have the listener attached as well -
Another Update
It seems that the issue might be with my repeated use of the function call which sets the event listener. But I don't understand why that would cause the issue, because I am not actually setting the event listener again.
The first time a label gets the event listener added, I give it a class called 'initialized' - anytime the function is called, it checks for that class first and if it doesn't exist, it adds the event listener.
I do this because I am also using the same code to initialize similar labels in an ajax loaded modal elsewhere in my app.
I don't understand why this would cause some kind of conflict.
Any ideas?