Event bubbling eventListener to remove/add class on target is not removing class











up vote
1
down vote

favorite












I am trying to add a class .active if the target element does not have that class, and if another element has that class I want to remove that class so only that class I clicked on has that element. I am trying to do this with this code -



document.addEventListener('click',function(e){
document.querySelector('.active').classList.remove('.active');
if(!e.target.classList.contains('active') && e.target.classList.contains('day')){
e.target.classList.add('active');
}
});


I expect that whatever has the .active class to be removed when I click on another element, but instead when I click on another element the active class remains on the element that already had the active class and it is added onto the target that I clicked on. I have no errors in my console.










share|improve this question






















  • document.querySelector('.active') will only select the first element with that class. You might want to check the API documentation when having problems with it: developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
    – connexo
    Nov 10 at 12:51












  • I only want the first element with that class to be selected because whatever the first element is, is what I last clicked on. I also tried document.getElementsByClassName('active')[0] but got the same results. I'm not sure what I should use to select the last element that I clicked on or whatever has the .active class.
    – kokokonoi
    Nov 10 at 12:52

















up vote
1
down vote

favorite












I am trying to add a class .active if the target element does not have that class, and if another element has that class I want to remove that class so only that class I clicked on has that element. I am trying to do this with this code -



document.addEventListener('click',function(e){
document.querySelector('.active').classList.remove('.active');
if(!e.target.classList.contains('active') && e.target.classList.contains('day')){
e.target.classList.add('active');
}
});


I expect that whatever has the .active class to be removed when I click on another element, but instead when I click on another element the active class remains on the element that already had the active class and it is added onto the target that I clicked on. I have no errors in my console.










share|improve this question






















  • document.querySelector('.active') will only select the first element with that class. You might want to check the API documentation when having problems with it: developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
    – connexo
    Nov 10 at 12:51












  • I only want the first element with that class to be selected because whatever the first element is, is what I last clicked on. I also tried document.getElementsByClassName('active')[0] but got the same results. I'm not sure what I should use to select the last element that I clicked on or whatever has the .active class.
    – kokokonoi
    Nov 10 at 12:52















up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am trying to add a class .active if the target element does not have that class, and if another element has that class I want to remove that class so only that class I clicked on has that element. I am trying to do this with this code -



document.addEventListener('click',function(e){
document.querySelector('.active').classList.remove('.active');
if(!e.target.classList.contains('active') && e.target.classList.contains('day')){
e.target.classList.add('active');
}
});


I expect that whatever has the .active class to be removed when I click on another element, but instead when I click on another element the active class remains on the element that already had the active class and it is added onto the target that I clicked on. I have no errors in my console.










share|improve this question













I am trying to add a class .active if the target element does not have that class, and if another element has that class I want to remove that class so only that class I clicked on has that element. I am trying to do this with this code -



document.addEventListener('click',function(e){
document.querySelector('.active').classList.remove('.active');
if(!e.target.classList.contains('active') && e.target.classList.contains('day')){
e.target.classList.add('active');
}
});


I expect that whatever has the .active class to be removed when I click on another element, but instead when I click on another element the active class remains on the element that already had the active class and it is added onto the target that I clicked on. I have no errors in my console.







javascript dom






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 12:48









kokokonoi

367




367












  • document.querySelector('.active') will only select the first element with that class. You might want to check the API documentation when having problems with it: developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
    – connexo
    Nov 10 at 12:51












  • I only want the first element with that class to be selected because whatever the first element is, is what I last clicked on. I also tried document.getElementsByClassName('active')[0] but got the same results. I'm not sure what I should use to select the last element that I clicked on or whatever has the .active class.
    – kokokonoi
    Nov 10 at 12:52




















  • document.querySelector('.active') will only select the first element with that class. You might want to check the API documentation when having problems with it: developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
    – connexo
    Nov 10 at 12:51












  • I only want the first element with that class to be selected because whatever the first element is, is what I last clicked on. I also tried document.getElementsByClassName('active')[0] but got the same results. I'm not sure what I should use to select the last element that I clicked on or whatever has the .active class.
    – kokokonoi
    Nov 10 at 12:52


















document.querySelector('.active') will only select the first element with that class. You might want to check the API documentation when having problems with it: developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
– connexo
Nov 10 at 12:51






document.querySelector('.active') will only select the first element with that class. You might want to check the API documentation when having problems with it: developer.mozilla.org/en-US/docs/Web/API/Document/querySelector
– connexo
Nov 10 at 12:51














I only want the first element with that class to be selected because whatever the first element is, is what I last clicked on. I also tried document.getElementsByClassName('active')[0] but got the same results. I'm not sure what I should use to select the last element that I clicked on or whatever has the .active class.
– kokokonoi
Nov 10 at 12:52






I only want the first element with that class to be selected because whatever the first element is, is what I last clicked on. I also tried document.getElementsByClassName('active')[0] but got the same results. I'm not sure what I should use to select the last element that I clicked on or whatever has the .active class.
– kokokonoi
Nov 10 at 12:52














1 Answer
1






active

oldest

votes

















up vote
3
down vote



accepted










It has to be



document.querySelector('.active').classList.remove('active')


not



document.querySelector('.active').classList.remove('.active')


As you already told the browser it's about CSS classes (classList), you no longer prefix the class you specify with a ..






share|improve this answer























  • thanks this was my problem and it works now
    – kokokonoi
    Nov 10 at 12:56











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239109%2fevent-bubbling-eventlistener-to-remove-add-class-on-target-is-not-removing-class%23new-answer', 'question_page');
}
);

Post as a guest
































1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
3
down vote



accepted










It has to be



document.querySelector('.active').classList.remove('active')


not



document.querySelector('.active').classList.remove('.active')


As you already told the browser it's about CSS classes (classList), you no longer prefix the class you specify with a ..






share|improve this answer























  • thanks this was my problem and it works now
    – kokokonoi
    Nov 10 at 12:56















up vote
3
down vote



accepted










It has to be



document.querySelector('.active').classList.remove('active')


not



document.querySelector('.active').classList.remove('.active')


As you already told the browser it's about CSS classes (classList), you no longer prefix the class you specify with a ..






share|improve this answer























  • thanks this was my problem and it works now
    – kokokonoi
    Nov 10 at 12:56













up vote
3
down vote



accepted







up vote
3
down vote



accepted






It has to be



document.querySelector('.active').classList.remove('active')


not



document.querySelector('.active').classList.remove('.active')


As you already told the browser it's about CSS classes (classList), you no longer prefix the class you specify with a ..






share|improve this answer














It has to be



document.querySelector('.active').classList.remove('active')


not



document.querySelector('.active').classList.remove('.active')


As you already told the browser it's about CSS classes (classList), you no longer prefix the class you specify with a ..







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 13:01

























answered Nov 10 at 12:54









connexo

19.1k63253




19.1k63253












  • thanks this was my problem and it works now
    – kokokonoi
    Nov 10 at 12:56


















  • thanks this was my problem and it works now
    – kokokonoi
    Nov 10 at 12:56
















thanks this was my problem and it works now
– kokokonoi
Nov 10 at 12:56




thanks this was my problem and it works now
– kokokonoi
Nov 10 at 12:56


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239109%2fevent-bubbling-eventlistener-to-remove-add-class-on-target-is-not-removing-class%23new-answer', 'question_page');
}
);

Post as a guest




















































































Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values