How to change a class to all divs?
up vote
0
down vote
favorite
I have this memory game I'm trying to add a button that will reveal all of the cards and flipped them again.
all the cards that are flipped are with class flipped. The problem happens when one of the cards is already flipped or hidden, then the function flips it as opposed to the other cards.
How can I fix that?
function revelCards() {
var revelCard = document.querySelectorAll('.card');
for (var i = 0; (i < revelCard.length) ; ++i) {
if (revelCard[i].classList.contains('flipped')){
revelCard[i].classList.remove('flipped');
} else {
revelCard[i].classList.add('flipped');
}
}
javascript
add a comment |
up vote
0
down vote
favorite
I have this memory game I'm trying to add a button that will reveal all of the cards and flipped them again.
all the cards that are flipped are with class flipped. The problem happens when one of the cards is already flipped or hidden, then the function flips it as opposed to the other cards.
How can I fix that?
function revelCards() {
var revelCard = document.querySelectorAll('.card');
for (var i = 0; (i < revelCard.length) ; ++i) {
if (revelCard[i].classList.contains('flipped')){
revelCard[i].classList.remove('flipped');
} else {
revelCard[i].classList.add('flipped');
}
}
javascript
without if statement?
– Martin Homola
Nov 11 at 22:38
1
What isrevelCards()
supposed to do? Should it reveal all cards no matter their current state?
– shkaper
Nov 11 at 22:43
At the very least you're missing a closing curly brace
– vol7ron
Nov 12 at 3:01
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have this memory game I'm trying to add a button that will reveal all of the cards and flipped them again.
all the cards that are flipped are with class flipped. The problem happens when one of the cards is already flipped or hidden, then the function flips it as opposed to the other cards.
How can I fix that?
function revelCards() {
var revelCard = document.querySelectorAll('.card');
for (var i = 0; (i < revelCard.length) ; ++i) {
if (revelCard[i].classList.contains('flipped')){
revelCard[i].classList.remove('flipped');
} else {
revelCard[i].classList.add('flipped');
}
}
javascript
I have this memory game I'm trying to add a button that will reveal all of the cards and flipped them again.
all the cards that are flipped are with class flipped. The problem happens when one of the cards is already flipped or hidden, then the function flips it as opposed to the other cards.
How can I fix that?
function revelCards() {
var revelCard = document.querySelectorAll('.card');
for (var i = 0; (i < revelCard.length) ; ++i) {
if (revelCard[i].classList.contains('flipped')){
revelCard[i].classList.remove('flipped');
} else {
revelCard[i].classList.add('flipped');
}
}
javascript
javascript
edited Nov 12 at 2:58
Shiladitya
9,39391830
9,39391830
asked Nov 11 at 22:34
Itzik Dabush
1
1
without if statement?
– Martin Homola
Nov 11 at 22:38
1
What isrevelCards()
supposed to do? Should it reveal all cards no matter their current state?
– shkaper
Nov 11 at 22:43
At the very least you're missing a closing curly brace
– vol7ron
Nov 12 at 3:01
add a comment |
without if statement?
– Martin Homola
Nov 11 at 22:38
1
What isrevelCards()
supposed to do? Should it reveal all cards no matter their current state?
– shkaper
Nov 11 at 22:43
At the very least you're missing a closing curly brace
– vol7ron
Nov 12 at 3:01
without if statement?
– Martin Homola
Nov 11 at 22:38
without if statement?
– Martin Homola
Nov 11 at 22:38
1
1
What is
revelCards()
supposed to do? Should it reveal all cards no matter their current state?– shkaper
Nov 11 at 22:43
What is
revelCards()
supposed to do? Should it reveal all cards no matter their current state?– shkaper
Nov 11 at 22:43
At the very least you're missing a closing curly brace
– vol7ron
Nov 12 at 3:01
At the very least you're missing a closing curly brace
– vol7ron
Nov 12 at 3:01
add a comment |
3 Answers
3
active
oldest
votes
up vote
1
down vote
You should simply remove the if
sentence, like this:
function revelCards() {
var revelCard = document.querySelectorAll('.card');
for (var i = 0; (i < revelCard.length) ; ++i) {
revelCard[i].classList.add('flipped');
}
}
Now flipped Cards will remain flipped and all others will flip.
add a comment |
up vote
0
down vote
The issue is that your loop just flips based on the current CSS.
However, one of them is flipped manually, so the loop just toggles the CSS.
To fix this, you could store the flipped value in a variable. Before going into the for loop, you could manually toggle (reset) the variable.
Alternatively, you could ignore the value that has been already set. This means that the CSS is not toggled again.
In either case, you would need a reference to the manually-modified original.
Thanks. How can i ignore the one that has been already set?
– Itzik Dabush
Nov 11 at 23:37
You could store the one flipped manually in a variable , when it is flipped (not , sure how you do it) for example:onClick(event){this.flipped = event.target };
then in the loop above, just checkif(card!==this.flipped) \do the class update
– dream88
Nov 12 at 10:25
add a comment |
up vote
0
down vote
If I understood it correctly, you have a set of "cards" whose content you want to show then hide. If I'm right, you could try doing something like this:
window.onload = ()=>{
let btn = document.getElementById('toggle');
btn.addEventListener('click', ()=>{
let cards = document.querySelectorAll('.card');
let flippedCards = ;
for(let i = 0; i < cards.length; i++){
// remember all the cards that are already flipped
if(cards[i].classList.contains('flipped')){
flippedCards.push(i);
}
// flip all the cards
cards[i].classList.add('flipped');
}
// flip all the cards again after 1 second (1000ms)
setTimeout(()=>{
for(let i = 0; i < cards.length; i++){
cards[i].classList.remove('flipped');
}
// return the cards to their original state
flippedCards.forEach(elem =>{
cards[elem].classList.add('flipped');
});
}, 1000);
});
};
.container {
display: flex;
margin-bottom: 5px;
}
.card {
transition: background-color 0.3s ease-in-out;
background-color: red;
width: 50px;
height: 50px;
margin-left: 5px;
}
.flipped {
background-color: green;
}
<div class="container">
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card flipped"></div>
<div class="card"></div>
<div class="card flipped"></div>
<div class="card"></div>
<div class="card"></div>
</div>
<input type="button" value="flip cards" id="toggle">
Also, here's a working example :)
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You should simply remove the if
sentence, like this:
function revelCards() {
var revelCard = document.querySelectorAll('.card');
for (var i = 0; (i < revelCard.length) ; ++i) {
revelCard[i].classList.add('flipped');
}
}
Now flipped Cards will remain flipped and all others will flip.
add a comment |
up vote
1
down vote
You should simply remove the if
sentence, like this:
function revelCards() {
var revelCard = document.querySelectorAll('.card');
for (var i = 0; (i < revelCard.length) ; ++i) {
revelCard[i].classList.add('flipped');
}
}
Now flipped Cards will remain flipped and all others will flip.
add a comment |
up vote
1
down vote
up vote
1
down vote
You should simply remove the if
sentence, like this:
function revelCards() {
var revelCard = document.querySelectorAll('.card');
for (var i = 0; (i < revelCard.length) ; ++i) {
revelCard[i].classList.add('flipped');
}
}
Now flipped Cards will remain flipped and all others will flip.
You should simply remove the if
sentence, like this:
function revelCards() {
var revelCard = document.querySelectorAll('.card');
for (var i = 0; (i < revelCard.length) ; ++i) {
revelCard[i].classList.add('flipped');
}
}
Now flipped Cards will remain flipped and all others will flip.
answered Nov 12 at 3:22
Poul Bak
5,42831132
5,42831132
add a comment |
add a comment |
up vote
0
down vote
The issue is that your loop just flips based on the current CSS.
However, one of them is flipped manually, so the loop just toggles the CSS.
To fix this, you could store the flipped value in a variable. Before going into the for loop, you could manually toggle (reset) the variable.
Alternatively, you could ignore the value that has been already set. This means that the CSS is not toggled again.
In either case, you would need a reference to the manually-modified original.
Thanks. How can i ignore the one that has been already set?
– Itzik Dabush
Nov 11 at 23:37
You could store the one flipped manually in a variable , when it is flipped (not , sure how you do it) for example:onClick(event){this.flipped = event.target };
then in the loop above, just checkif(card!==this.flipped) \do the class update
– dream88
Nov 12 at 10:25
add a comment |
up vote
0
down vote
The issue is that your loop just flips based on the current CSS.
However, one of them is flipped manually, so the loop just toggles the CSS.
To fix this, you could store the flipped value in a variable. Before going into the for loop, you could manually toggle (reset) the variable.
Alternatively, you could ignore the value that has been already set. This means that the CSS is not toggled again.
In either case, you would need a reference to the manually-modified original.
Thanks. How can i ignore the one that has been already set?
– Itzik Dabush
Nov 11 at 23:37
You could store the one flipped manually in a variable , when it is flipped (not , sure how you do it) for example:onClick(event){this.flipped = event.target };
then in the loop above, just checkif(card!==this.flipped) \do the class update
– dream88
Nov 12 at 10:25
add a comment |
up vote
0
down vote
up vote
0
down vote
The issue is that your loop just flips based on the current CSS.
However, one of them is flipped manually, so the loop just toggles the CSS.
To fix this, you could store the flipped value in a variable. Before going into the for loop, you could manually toggle (reset) the variable.
Alternatively, you could ignore the value that has been already set. This means that the CSS is not toggled again.
In either case, you would need a reference to the manually-modified original.
The issue is that your loop just flips based on the current CSS.
However, one of them is flipped manually, so the loop just toggles the CSS.
To fix this, you could store the flipped value in a variable. Before going into the for loop, you could manually toggle (reset) the variable.
Alternatively, you could ignore the value that has been already set. This means that the CSS is not toggled again.
In either case, you would need a reference to the manually-modified original.
edited Nov 12 at 2:53
Avery
4915
4915
answered Nov 11 at 23:02
dream88
1307
1307
Thanks. How can i ignore the one that has been already set?
– Itzik Dabush
Nov 11 at 23:37
You could store the one flipped manually in a variable , when it is flipped (not , sure how you do it) for example:onClick(event){this.flipped = event.target };
then in the loop above, just checkif(card!==this.flipped) \do the class update
– dream88
Nov 12 at 10:25
add a comment |
Thanks. How can i ignore the one that has been already set?
– Itzik Dabush
Nov 11 at 23:37
You could store the one flipped manually in a variable , when it is flipped (not , sure how you do it) for example:onClick(event){this.flipped = event.target };
then in the loop above, just checkif(card!==this.flipped) \do the class update
– dream88
Nov 12 at 10:25
Thanks. How can i ignore the one that has been already set?
– Itzik Dabush
Nov 11 at 23:37
Thanks. How can i ignore the one that has been already set?
– Itzik Dabush
Nov 11 at 23:37
You could store the one flipped manually in a variable , when it is flipped (not , sure how you do it) for example:
onClick(event){this.flipped = event.target };
then in the loop above, just check if(card!==this.flipped) \do the class update
– dream88
Nov 12 at 10:25
You could store the one flipped manually in a variable , when it is flipped (not , sure how you do it) for example:
onClick(event){this.flipped = event.target };
then in the loop above, just check if(card!==this.flipped) \do the class update
– dream88
Nov 12 at 10:25
add a comment |
up vote
0
down vote
If I understood it correctly, you have a set of "cards" whose content you want to show then hide. If I'm right, you could try doing something like this:
window.onload = ()=>{
let btn = document.getElementById('toggle');
btn.addEventListener('click', ()=>{
let cards = document.querySelectorAll('.card');
let flippedCards = ;
for(let i = 0; i < cards.length; i++){
// remember all the cards that are already flipped
if(cards[i].classList.contains('flipped')){
flippedCards.push(i);
}
// flip all the cards
cards[i].classList.add('flipped');
}
// flip all the cards again after 1 second (1000ms)
setTimeout(()=>{
for(let i = 0; i < cards.length; i++){
cards[i].classList.remove('flipped');
}
// return the cards to their original state
flippedCards.forEach(elem =>{
cards[elem].classList.add('flipped');
});
}, 1000);
});
};
.container {
display: flex;
margin-bottom: 5px;
}
.card {
transition: background-color 0.3s ease-in-out;
background-color: red;
width: 50px;
height: 50px;
margin-left: 5px;
}
.flipped {
background-color: green;
}
<div class="container">
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card flipped"></div>
<div class="card"></div>
<div class="card flipped"></div>
<div class="card"></div>
<div class="card"></div>
</div>
<input type="button" value="flip cards" id="toggle">
Also, here's a working example :)
add a comment |
up vote
0
down vote
If I understood it correctly, you have a set of "cards" whose content you want to show then hide. If I'm right, you could try doing something like this:
window.onload = ()=>{
let btn = document.getElementById('toggle');
btn.addEventListener('click', ()=>{
let cards = document.querySelectorAll('.card');
let flippedCards = ;
for(let i = 0; i < cards.length; i++){
// remember all the cards that are already flipped
if(cards[i].classList.contains('flipped')){
flippedCards.push(i);
}
// flip all the cards
cards[i].classList.add('flipped');
}
// flip all the cards again after 1 second (1000ms)
setTimeout(()=>{
for(let i = 0; i < cards.length; i++){
cards[i].classList.remove('flipped');
}
// return the cards to their original state
flippedCards.forEach(elem =>{
cards[elem].classList.add('flipped');
});
}, 1000);
});
};
.container {
display: flex;
margin-bottom: 5px;
}
.card {
transition: background-color 0.3s ease-in-out;
background-color: red;
width: 50px;
height: 50px;
margin-left: 5px;
}
.flipped {
background-color: green;
}
<div class="container">
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card flipped"></div>
<div class="card"></div>
<div class="card flipped"></div>
<div class="card"></div>
<div class="card"></div>
</div>
<input type="button" value="flip cards" id="toggle">
Also, here's a working example :)
add a comment |
up vote
0
down vote
up vote
0
down vote
If I understood it correctly, you have a set of "cards" whose content you want to show then hide. If I'm right, you could try doing something like this:
window.onload = ()=>{
let btn = document.getElementById('toggle');
btn.addEventListener('click', ()=>{
let cards = document.querySelectorAll('.card');
let flippedCards = ;
for(let i = 0; i < cards.length; i++){
// remember all the cards that are already flipped
if(cards[i].classList.contains('flipped')){
flippedCards.push(i);
}
// flip all the cards
cards[i].classList.add('flipped');
}
// flip all the cards again after 1 second (1000ms)
setTimeout(()=>{
for(let i = 0; i < cards.length; i++){
cards[i].classList.remove('flipped');
}
// return the cards to their original state
flippedCards.forEach(elem =>{
cards[elem].classList.add('flipped');
});
}, 1000);
});
};
.container {
display: flex;
margin-bottom: 5px;
}
.card {
transition: background-color 0.3s ease-in-out;
background-color: red;
width: 50px;
height: 50px;
margin-left: 5px;
}
.flipped {
background-color: green;
}
<div class="container">
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card flipped"></div>
<div class="card"></div>
<div class="card flipped"></div>
<div class="card"></div>
<div class="card"></div>
</div>
<input type="button" value="flip cards" id="toggle">
Also, here's a working example :)
If I understood it correctly, you have a set of "cards" whose content you want to show then hide. If I'm right, you could try doing something like this:
window.onload = ()=>{
let btn = document.getElementById('toggle');
btn.addEventListener('click', ()=>{
let cards = document.querySelectorAll('.card');
let flippedCards = ;
for(let i = 0; i < cards.length; i++){
// remember all the cards that are already flipped
if(cards[i].classList.contains('flipped')){
flippedCards.push(i);
}
// flip all the cards
cards[i].classList.add('flipped');
}
// flip all the cards again after 1 second (1000ms)
setTimeout(()=>{
for(let i = 0; i < cards.length; i++){
cards[i].classList.remove('flipped');
}
// return the cards to their original state
flippedCards.forEach(elem =>{
cards[elem].classList.add('flipped');
});
}, 1000);
});
};
.container {
display: flex;
margin-bottom: 5px;
}
.card {
transition: background-color 0.3s ease-in-out;
background-color: red;
width: 50px;
height: 50px;
margin-left: 5px;
}
.flipped {
background-color: green;
}
<div class="container">
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card flipped"></div>
<div class="card"></div>
<div class="card flipped"></div>
<div class="card"></div>
<div class="card"></div>
</div>
<input type="button" value="flip cards" id="toggle">
Also, here's a working example :)
window.onload = ()=>{
let btn = document.getElementById('toggle');
btn.addEventListener('click', ()=>{
let cards = document.querySelectorAll('.card');
let flippedCards = ;
for(let i = 0; i < cards.length; i++){
// remember all the cards that are already flipped
if(cards[i].classList.contains('flipped')){
flippedCards.push(i);
}
// flip all the cards
cards[i].classList.add('flipped');
}
// flip all the cards again after 1 second (1000ms)
setTimeout(()=>{
for(let i = 0; i < cards.length; i++){
cards[i].classList.remove('flipped');
}
// return the cards to their original state
flippedCards.forEach(elem =>{
cards[elem].classList.add('flipped');
});
}, 1000);
});
};
.container {
display: flex;
margin-bottom: 5px;
}
.card {
transition: background-color 0.3s ease-in-out;
background-color: red;
width: 50px;
height: 50px;
margin-left: 5px;
}
.flipped {
background-color: green;
}
<div class="container">
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card flipped"></div>
<div class="card"></div>
<div class="card flipped"></div>
<div class="card"></div>
<div class="card"></div>
</div>
<input type="button" value="flip cards" id="toggle">
window.onload = ()=>{
let btn = document.getElementById('toggle');
btn.addEventListener('click', ()=>{
let cards = document.querySelectorAll('.card');
let flippedCards = ;
for(let i = 0; i < cards.length; i++){
// remember all the cards that are already flipped
if(cards[i].classList.contains('flipped')){
flippedCards.push(i);
}
// flip all the cards
cards[i].classList.add('flipped');
}
// flip all the cards again after 1 second (1000ms)
setTimeout(()=>{
for(let i = 0; i < cards.length; i++){
cards[i].classList.remove('flipped');
}
// return the cards to their original state
flippedCards.forEach(elem =>{
cards[elem].classList.add('flipped');
});
}, 1000);
});
};
.container {
display: flex;
margin-bottom: 5px;
}
.card {
transition: background-color 0.3s ease-in-out;
background-color: red;
width: 50px;
height: 50px;
margin-left: 5px;
}
.flipped {
background-color: green;
}
<div class="container">
<div class="card"></div>
<div class="card"></div>
<div class="card"></div>
<div class="card flipped"></div>
<div class="card"></div>
<div class="card flipped"></div>
<div class="card"></div>
<div class="card"></div>
</div>
<input type="button" value="flip cards" id="toggle">
answered Nov 12 at 5:08
C.RaysOfTheSun
56527
56527
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53253927%2fhow-to-change-a-class-to-all-divs%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
without if statement?
– Martin Homola
Nov 11 at 22:38
1
What is
revelCards()
supposed to do? Should it reveal all cards no matter their current state?– shkaper
Nov 11 at 22:43
At the very least you're missing a closing curly brace
– vol7ron
Nov 12 at 3:01