Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide one div and show another div using button onclick?

I have two div in my html file. I want to hide the 1st div and show another div on html input button onclick event.

Here is my code,

function switchVisible() {
  if (document.getElementById('Div1') !== undefined) {
    if (document.getElementById('Div1').style.display == 'Block') {
      document.getElementById('Div1').style.display = 'none';
      document.getElementById('Div2').style.display = 'Block';
    } else {
      document.getElementById('Div1').style.display = 'Block';
      document.getElementById('Div2').style.display = 'none';
    }
  }
}
#Div2 {
  display: none;
}
<input id="Button1" type="button" value="" onclick="javascript:switchVisible();" />

But it's not working. Any help will be appreciated.

Thanks.

like image 327
Arpita Avatar asked Sep 22 '14 18:09

Arpita


4 Answers

1) Inside onclick, you don't have to use "javascript:", that is implied.

2) You check for "display: block", I always check for "display: none" (Because the display can also be "inline-block", etc.)

Try this:

function switchVisible() {
            if (document.getElementById('Div1')) {

                if (document.getElementById('Div1').style.display == 'none') {
                    document.getElementById('Div1').style.display = 'block';
                    document.getElementById('Div2').style.display = 'none';
                }
                else {
                    document.getElementById('Div1').style.display = 'none';
                    document.getElementById('Div2').style.display = 'block';
                }
            }
}
#Div2 {
  display: none;
}
<div id="Div1">Div 1</div>
<div id="Div2">Div 2</div>

<input id="Button1" type="button" value="Click" onclick="switchVisible();"/>
like image 101
Philip Avatar answered Oct 15 '22 17:10

Philip


As it is tagged with jQuery, here's the simple jQuery answer

$('body').on("click touchstart", "#Button1", function(e){
   $("#Div1, #Div2").toggle();
});

use on to listen for the id #Button, I've used both click and touchstart to make it mobile friendly, and then used toggle() which sets the state of the display: to the opposite to what it is now. So if it was display:none, it becomes display:block, if it was display:block it becomes display:none

like image 33
Andrew Killen Avatar answered Oct 15 '22 16:10

Andrew Killen


Try this:

var div1 = document.getElementById('Div1'),
    div2 = document.getElementById('Div2');
function switchVisible() {
  if(!div1) return;
  if (getComputedStyle(div1).display == 'block') {
    div1.style.display = 'none';
    div2.style.display = 'block';
  } else {
    div1.style.display = 'block';
    div2.style.display = 'none';
  }
}
document.getElementById('Button1').addEventListener('click', switchVisible);
#Div2 {
  display:none;
}
<div id="Div1">Div 1</div>
<div id="Div2">Div 2</div>
<input id="Button1" type="button" value="Click me" />

However, this approach may be better:

var wrapper = document.getElementById('wrapper');
function switchVisible() {
  wrapper.classList.toggle('switched');
}
document.getElementById('Button1').addEventListener('click', switchVisible);
#wrapper > :last-child {
  display: none;
}
#wrapper.switched > :last-child {
  display: block;
}
#wrapper.switched > :first-child {
  display: none;
}
<div id="wrapper">
  <div>Div 1</div>
  <div>Div 2</div>
</div>
<input id="Button1" type="button" value="Click me" />
like image 23
Oriol Avatar answered Oct 15 '22 17:10

Oriol


you might wanna try this..

    function switchVisible() {
    	var div1=document.getElementById('Div1');
    	var div2=document.getElementById('Div2');
    	
      if (div1 !== undefined && div2 !== undefined) {
    	  div1.style.display = div2.style.display === '' ? 'none' : div2.style.display === 'none' ? 'none' : 'block';
    	  div2.style.display = div1.style.display === 'block' ? 'none' : 'block';
      }
    }
#Div1{
 display: block;
 background: blue;
}

#Div2 {
  display: none;
  background: red;
}

.divs
{
 width: 50px;
 height: 50px;
 border: 1px solid #000;
}
<input id="Button1" type="button" value="Hide" onclick="switchVisible();" />

<div id="Div1" class="divs"> </div>
<div id="Div2" class="divs"> </div>
like image 45
Wracker Avatar answered Oct 15 '22 18:10

Wracker