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');











share|improve this question























  • 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














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');











share|improve this question























  • 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












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');











share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 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
















  • 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















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












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.






share|improve this answer



























    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.






    share|improve this answer






















    • 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


















    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 :)






    share|improve this answer




















      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%2f53253927%2fhow-to-change-a-class-to-all-divs%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      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.






      share|improve this answer
























        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.






        share|improve this answer






















          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.






          share|improve this answer












          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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 12 at 3:22









          Poul Bak

          5,42831132




          5,42831132






















              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.






              share|improve this answer






















              • 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















              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.






              share|improve this answer






















              • 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













              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.






              share|improve this answer














              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.







              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Nov 12 at 2:53









              Avery

              4915




              4915










              answered Nov 11 at 23:02









              dream88

              1308




              1308











              • 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

















              • 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
















              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











              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 :)






              share|improve this answer
























                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 :)






                share|improve this answer






















                  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 :)






                  share|improve this answer












                  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">






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 12 at 5:08









                  C.RaysOfTheSun

                  56527




                  56527



























                      draft saved

                      draft discarded
















































                      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.




                      draft saved


                      draft discarded














                      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





















































                      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







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      政党

                      天津地下鉄3号線