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.
javascript dom
add a comment |
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.
javascript dom
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
add a comment |
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.
javascript dom
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
javascript dom
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
add a comment |
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
add a comment |
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 .
.
thanks this was my problem and it works now
– kokokonoi
Nov 10 at 12:56
add a comment |
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 .
.
thanks this was my problem and it works now
– kokokonoi
Nov 10 at 12:56
add a comment |
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 .
.
thanks this was my problem and it works now
– kokokonoi
Nov 10 at 12:56
add a comment |
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 .
.
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 .
.
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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