Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide & show content with javaScript

So, on my example I have 2 div-buttons (named btn1 and btn2) and 2 div elements (named content1 and content2).

What I would want, is that when you click the btn1, content1 shows. If you click btn2, content2 should show.

Content1 and content2 elements are currently placed in the same position, and by default, none of the content elements shouldn't be open before you have clicked anything. I would like to achieve this with pure javaSript.

Here is the example code:

var btn1 = document.getElementById("btn1");
var content1 = document.getElementById("content1");
content1.style.opacity = "0";

btn1.addEventListener("mouseover", showContent1);

function showContent1(){
  if(content1.style.opacity === "0") {
    content1.style.opacity = "1";
  } else {content1.style.opacity = "0";}
}

var btn2 = document.getElementById("btn2");
var content2 = document.getElementById("content2");
content2.style.opacity = "0";

btn2.addEventListener("mouseover", showContent2);

function showContent2(){
  if(content2.style.opacity === "0") {
    content2.style.opacity = "1";
  } else {content2.style.opacity = "0";}
}
#btn1, #btn2 {
width:100px;height:20px;text-align:center;background:grey;cursor:pointer;margin:10px 0px;
}
#contents {
width: 200px;height:200px;border: 2px solid black;
}
#content1, #content2 {
width: 200px;height:200px;position:absolute;background:lightblue;
}
<div id="btn1">show1</div>
<div id="btn2">show2</div>
<div id="contents">
  <div id="content1">content 1</div>
  <div id="content2">content 2</div>
</div>
like image 235
Tane Avatar asked May 16 '26 16:05

Tane


2 Answers

You can add click event to the buttons and based on the button clicked you can show or hide the respective div.

<!DOCTYPE html>
    <html>

      <head>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>

    <style>
      #btn1, #btn2 {
width:100px;height:20px;text-align:center;background:grey;cursor:pointer;margin:10px 0px;
}
#contents {
width: 200px;
height:200px;
border: 2px solid black;
}
#content1, #content2 {
width: 200px;
height:200px;
position:absolute;
background:lightblue;
display:none;
}
    </style>
  </head>

  <body>

    <script>
      function showDiv(div){

        if(div == 'btn1'){
          document.getElementById('content1').style.display = 'block';
          document.getElementById('content2').style.display = 'none';
        }else{
          document.getElementById('content1').style.display = 'none';
          document.getElementById('content2').style.display = 'block';
        }
      }
    </script>
    <div id="btn1" onclick="showDiv('btn1')">show1</div>
<div id="btn2" onclick="showDiv('btn2')">show2</div>


<div id="contents">
  <div id="content1">content 1</div>
  <div id="content2">content 2</div>
</div>
  </body>

Plunker For the same: https://plnkr.co/edit/brxoF2ClW2TeJOVMxn8d?p=preview

like image 107
Amit Avatar answered May 18 '26 06:05

Amit


Check this, i've made it dynamic so you can create unlimited buttons and contents.

function toogleContent(id){
  var toogleContent = document.getElementsByClassName('toogleContent');
  var i = toogleContent.length;
  while (i--) toogleContent[i].style.display = "none";
  document.getElementById(id).style.display = "block";
}
#btn1, #btn2 {
  width:100px;
  height:20px;
  text-align:center;
  background:grey;
  cursor:pointer;
  margin:10px 0px;
}
#contents {
  width: 200px;
  height:200px;
  border: 2px solid black;
}
#content1, #content2 {
  width: 200px;
  height:200px;
  position:absolute;
  background:lightblue;
  display:none;
}
<div id="btn1" class="toogleBtn" onclick="toogleContent('content1')">show1</div>
<div id="btn2" class="toogleBtn" onclick="toogleContent('content2')">show2</div>
<div id="contents">
  <div id="content1" class="toogleContent">content 1</div>
  <div id="content2" class="toogleContent">content 2</div>
</div>
like image 22
Ahmed Tag Amer Avatar answered May 18 '26 05:05

Ahmed Tag Amer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!