Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

JavaScript JavaScript Hiding All Images On Page But I Need One Always Visible

Hi!

I have this page which displays different shield colors, depending on which TEXT is clicked (Bronze, Silver, Gold, Pro or Legend).

By default, the BRONZE shield image is showing along with the country flag image:
0F4JI.png


Then when you click on Silver, Gold, Pro or Legend, the corresponding shield image appears - below the SILVER one for example, but the country flag (a separate image) disappears:
VeLZy.png

My code follows below. I know that the javascript code hides all images that is not the selected one, and that is why the country flag disappears. But I am unable to figure out how to single out the flag image to make it visible again afterwards, or to keep it always visible. I am relatively new to coding and still learning - any help would be much appreciated. Thank you in advance!

HTML:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>A2B Stat Track League</title>
    
    <!-- Template Stylesheet -->
   <link href="css/style.css" rel="stylesheet">

</head>
<body>
    
    <header>
    
        <h4>Stat Track League</h4>
        <h3>Cards</h3>

    </header>
    
    <div class="container">
            
        <ul>
            <li><a data-img="card-bronze" id="bronze" href="#">Bronze</a></li>
            <li><a data-img="card-silver" id="silver" href="#">Silver</a></li>
            <li><a data-img="card-gold" id="gold" href="#">Gold</a></li>
            <li><a data-img="card-pro" id="pro" href="#">Pro</a></li>
            <li><a data-img="card-legend" id="legend" href="#">Legend</a></li>
        </ul>
    
        <img class="show" id="card-bronze" src="img/card-bronze.png">
        <img class="hide" id="card-silver" src="img/card-silver.png">
        <img class="hide" id="card-gold" src="img/card-gold.png">
        <img class="hide" id="card-pro" src="img/card-pro.png">
        <img class="hide" id="card-legend" src="img/card-legend.png">
        
        <div>
            <img class="flag" src="img/flags/portugal.png">
        </div>
        
    </div>

    <!-- Template Javascript -->
    <script src="js/main.js"></script>

</body>
</html>

JavaScript:
var bronze = document.getElementById("bronze");
var silver = document.getElementById("silver");
var gold = document.getElementById("gold");
var pro = document.getElementById("pro");
var legend = document.getElementById("legend");

bronze.addEventListener("click", picLink);
silver.addEventListener("click", picLink);
gold.addEventListener("click", picLink);
pro.addEventListener("click", picLink);
legend.addEventListener("click", picLink);

function picLink() {
  var allImages = document.querySelectorAll("img");

  for (var i = 0; i < allImages.length; i++) {
    allImages[i].className = "hide";
  }

  var picID = this.attributes["data-img"].value;
  var pic = document.getElementById(picID);
  if (pic.className === "hide") {
    pic.className = "";
  } else {
    pic.className = "hide";
  }
}
 
Solution
JavaScript:
for (let i = 0; i < allImages.length; i++) {
    if( allImages[i].className == "flag" ) continue;
    else{allImages[i].className = "hide";}
  }
Hi!

I have this page which displays different shield colors, depending on which TEXT is clicked (Bronze, Silver, Gold, Pro or Legend).

By default, the BRONZE shield image is showing along with the country flag image:
0F4JI.png


Then when you click on Silver, Gold, Pro or Legend, the corresponding shield image appears - below the SILVER one for example, but the country flag (a separate image) disappears:
VeLZy.png

My code follows below. I know that the javascript code hides all images that is not the selected one, and that is why the country flag disappears. But I am unable to figure out how to single out the flag image to make it visible again afterwards, or to keep it always visible. I am relatively new to coding and still learning - any help would be much appreciated. Thank you in advance!

HTML:
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>A2B Stat Track League</title>
   
    <!-- Template Stylesheet -->
   <link href="css/style.css" rel="stylesheet">

</head>
<body>
   
    <header>
   
        <h4>Stat Track League</h4>
        <h3>Cards</h3>

    </header>
   
    <div class="container">
           
        <ul>
            <li><a data-img="card-bronze" id="bronze" href="#">Bronze</a></li>
            <li><a data-img="card-silver" id="silver" href="#">Silver</a></li>
            <li><a data-img="card-gold" id="gold" href="#">Gold</a></li>
            <li><a data-img="card-pro" id="pro" href="#">Pro</a></li>
            <li><a data-img="card-legend" id="legend" href="#">Legend</a></li>
        </ul>
   
        <img class="show" id="card-bronze" src="img/card-bronze.png">
        <img class="hide" id="card-silver" src="img/card-silver.png">
        <img class="hide" id="card-gold" src="img/card-gold.png">
        <img class="hide" id="card-pro" src="img/card-pro.png">
        <img class="hide" id="card-legend" src="img/card-legend.png">
       
        <div>
            <img class="flag" src="img/flags/portugal.png">
        </div>
       
    </div>

    <!-- Template Javascript -->
    <script src="js/main.js"></script>

</body>
</html>

JavaScript:
var bronze = document.getElementById("bronze");
var silver = document.getElementById("silver");
var gold = document.getElementById("gold");
var pro = document.getElementById("pro");
var legend = document.getElementById("legend");

bronze.addEventListener("click", picLink);
silver.addEventListener("click", picLink);
gold.addEventListener("click", picLink);
pro.addEventListener("click", picLink);
legend.addEventListener("click", picLink);

function picLink() {
  var allImages = document.querySelectorAll("img");

  for (var i = 0; i < allImages.length; i++) {
    allImages[i].className = "hide";
  }

  var picID = this.attributes["data-img"].value;
  var pic = document.getElementById(picID);
  if (pic.className === "hide") {
    pic.className = "";
  } else {
    pic.className = "hide";
  }
}
Hi there,
So here's a question to help guide the answer... Does the flag image ever need to be hidden? If so, what are the conditions for it to be hidden?
 
Thank you Padonak for the reply!

I replaced this:

Code:
for (var i = 0; i < allImages.length; i++) {
  allImages[i].className = "hide";
}

With this as you suggested:

Code:
for (let i = 0; i < allImages.length; i++) {
  if( allImages[i].className == "flag" ) continue;
  else{allImages[i].className = "hide";}
}

And it worked! Thank you very very much!
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom