Home > Enterprise >  Javascript hide/show div when the next button is clicked
Javascript hide/show div when the next button is clicked

Time:05-14

I have a page with many images acting as button that when clicked on, reveal a seperate set of images. However, I only want one set to be shown at a time -- so that when you click on the next button, it hides the set currently being shown. I'm sure this isn't too tricky, but I'm such a beginner! Any help would be greatly appreciated. Here is a snippet of my code:

HTML:

<button onclick="contentcarFunction()">
            <div  id="carprint">
                <img  src="img/carprint-400px.png" alt="">
            </div> 
        </button>

        <div  id="carprint-1">
            <img  id="content" src="img/CARPRINT/carprint1.jpeg" alt="">
            <img  id="content" src="img/CARPRINT/carprint2.jpeg" alt="">
        </div>

        <button onclick="contentyanaFunction()">
            <div  id="yana">
                <img  src="img/yana-400px.png" alt="">
            </div>
        </button>

        <div  id="yana-1">
            <img  id="content" src="img/DVD/DVDV.jpg" alt="">
            <img  id="content" src="img/DVD/Screen Shot 2020-04-15 at 14.02.47.png" alt="">
        </div>

        <button onclick="contenttsarFunction()">
            <div  id="tsar">
                <img  src="img/tsar1-400px.png" alt="">
            </div>
        </button>

        <div  id="tsar-1">
            <iframe id="clip" width="560" height="315" src="https://www.youtube.com/embed/wQgjq-Hiz4Y"></iframe>
            <img  id="content" src="img/Tsar/Excursion.gif" alt="">
            <img  id="content" src="img/Tsar/Lecture.gif" alt="">
        </div>

JAVA:

function contentcarFunction() {
  var x = document.getElementById("carprint-1");
    if (x.style.display === "none") {
      x.style.display = "block";
    } else {
      x.style.display = "none";
  }
}

function contentyanaFunction() {
  var x = document.getElementById("yana-1");
    if (x.style.display === "none") {
      x.style.display = "block";
    } else {
      x.style.display = "none";
  }
}

function contenttsarFunction() {
  var x = document.getElementById("tsar-1");
    if (x.style.display === "none") {
      x.style.display = "block";
    } else {
      x.style.display = "none";
  }
}

CodePudding user response:

You can set display none of the other btn content when one btn is click

const carPrint = document.getElementById("carprint-1");
const yana = document.getElementById("yana-1");
const tsar = document.getElementById("tsar-1");

function contentcarFunction() {
  if (carPrint.style.display === "none") {
    carPrint.style.display = "block";
    yana.style.display = "none";
    tsar.style.display = "none";
  } else {
    carPrint.style.display = "none";
  }
}

function contentyanaFunction() {
  if (yana.style.display === "none") {
    yana.style.display = "block";
    tsar.style.display = "none";
    carPrint.style.display = "none";
  } else {
    yana.style.display = "none";
  }
}

function contenttsarFunction() {
  if (tsar.style.display === "none") {
    tsar.style.display = "block";
    yana.style.display = "none";
    carPrint.style.display = "none";
  } else {
    tsar.style.display = "none";
  }
}
#carprint-1,
#yana-1,
#tsar-1 {
  display: none;
}
<button onclick="contentcarFunction()">
    <div  id="carprint">
        Car Print
    </div>
</button>

<div  id="carprint-1">
  Car Print Content
</div>

<button onclick="contentyanaFunction()">
    <div  id="yana">
        Yana
    </div>
</button>

<div  id="yana-1">
  Yana Content
</div>

<button onclick="contenttsarFunction()">
    <div  id="tsar">
        Tsar
    </div>
</button>

<div  id="tsar-1">
  Tsar Content

</div>

  • Related