Bhavin Shah Bhavin Shah - 1 year ago 59
CSS Question

Show One Div and Hide Another Div when Click on button

What I want to do is, when I click on button then one div should disappear and another should appear and if i click on the same button one more time, then first one should appear and second one should disappear. This would go until I click on the button.

What I tried:


function change_image(){
var flag = true;
flag = false;
} else {
flag = true;


<input type="button" value="Click Here to get another image" onClick="change_image(this)">

Any help would be grateful.

Thank You.

Answer Source

Your flag variable is local, and its value is always the same when function is called. Initialize it with:

var flag = document.getElementById('speaker_main_div_with_rounded_image').style.display !== 'none';
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download