gridle
gridle

Reputation: 21

Hide/show cards in bootstrap

Im trying to hide and show a few cards in bootstrap, but I can't figure it out. All cards have the class card (of course) and im trying to hide all those cards when a button is clicked. Here is what I have now:

function myFunction() {

  jQuery(document).ready(function($) {
    $(".card").hide();
  });

  var game = document.getElementById("game").value;
  var resolution = document.getElementById("resolution").value;
  var graphic = document.getElementById("graphic").value;

  if (game == "Black" && graphic == "high" && resolution == "1080") {
    alert("Hello " + game + "! You will now be redirected to www.w3Schools.com");
  } else if (book == "Red") {

  } else if (book == "Green") {

  } else {

  }
}

The call for the function is correct cause the alert does work properly.

For some reason the

jQuery(document).ready(function($) {
        $(".card").hide();
      });

part does work when outside the js function (when it's not connected to the button).

No idea if it helps but here is also a snipped of my bootstrap doc:

<button type="submit" class="btn btn-primary" id="btn" onclick="myFunction()">Submit</button>
          </form>
        </div>
        <!-- Results -->
        <div class="card" id="p2" style="width:200px; margin:30px">
          <img class="card-img-top" src="https://image" alt="Card image" style="width:100%">
          <div class="card-body">
            <h5 class="card-title">Processor</h5>
            <p><a href="#">Newegg</a></p>
            <p><a href="#">Newegg</a></p>
            <p><a href="#">Newegg</a></p>
          </div>
        </div>
        <div class="card" id="p3" style="width:200px; margin:30px">
          <img class="card-img-top" src="https://image" alt="Card image" style="width:100%">
          <div class="card-body">
            <h5 class="card-title">Graphic card</h5>
            <p><a href="#">Newegg</a></p>
            <p><a href="#">Newegg</a></p>
            <p><a href="#">Newegg</a></p>
          </div>
        </div>

Here the things I've tried already:

The toggle How to hide and show bootstrap 4 cards by hovering over navigation menu through css?

Standard js document.getElementById(".card").style.display = "none";

I've looked at the react stuff, but I don't understand that.

Upvotes: 2

Views: 11317

Answers (1)

jhimelymendoza
jhimelymendoza

Reputation: 11

I think what you have to do is this if you want to make a show and hide toggle of all the elements that have the card class in your DOM.

var myFunction = function() {

 var divsToHide = document.getElementsByClassName("card"); 
if(divsToHide.length>0){
        for(var i = 0; i < divsToHide.length; i++){

      if( divsToHide[i].style.display== "none"){
    divsToHide[i].style.display = "block";
      }else{

           divsToHide[i].style.display = "none"; // depending on what you're doing
      }    
}} }

I hope it helps you

Upvotes: 1

Related Questions