Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show/hide div's with javascript on a button press (and have all div's hidden first)

I am new to javascript and I cant seam to hide the div's to start with, I can get the divs to switch between each other any help would be great

<script type="text/javascript">
function show(elementId) { 
 document.getElementById("id1").style.display="none";
 document.getElementById("id2").style.display="none";
 document.getElementById("id3").style.display="none";
 document.getElementById(elementId).style.display="block";
}
</script>


<button type="button" onclick="show('id1');">Button 1</button>
<button type="button" onclick="show('id2');">Button 2</button>
<button type="button" onclick="show('id3');">Button 3</button>

<div id="id1">text 1</div>
<div id="id2">text 2</div>
<div id="id3">text 3</div>
like image 926
Obon Avatar asked Feb 12 '14 14:02

Obon


3 Answers

You can hide all the divs by adding inline styles:

<script type="text/javascript">
function show(elementId) { 
 document.getElementById("id1").style.display="none";
 document.getElementById("id2").style.display="none";
 document.getElementById("id3").style.display="none";
 document.getElementById(elementId).style.display="block";
}
</script>


<button type="button" onclick="show('id1');">Button 1</button>
<button type="button" onclick="show('id2');">Button 2</button>
<button type="button" onclick="show('id3');">Button 3</button>

<div id="id1"  style="display:none">text 1</div>
<div id="id2"  style="display:none">text 2</div>
<div id="id3"  style="display:none">text 3</div>

See it working here: http://jsbin.com/suhok/2/

like image 144
Emilio Rodriguez Avatar answered Nov 19 '22 18:11

Emilio Rodriguez


<script type="text/javascript">
function show(elementId) { 
 document.getElementById("id1").style.display="none";
 document.getElementById("id2").style.display="none";
 document.getElementById("id3").style.display="none";
 document.getElementById(elementId).style.display="block";
}
</script>
<style>
 div{
  display:none;
 }
</style>

<button type="button" onclick="show('id1');">Button 1</button>
<button type="button" onclick="show('id2');">Button 2</button>
<button type="button" onclick="show('id3');">Button 3</button>

<div id="id1">text 1</div>
<div id="id2">text 2</div>
<div id="id3">text 3</div>

All the div's will be hidden on the first go, you code will work this way, put buttons and div's under <body> tag and <script> and <style> under <head> tag

here is JSFindle Link

like image 3
Hitesh Kumar Avatar answered Nov 19 '22 17:11

Hitesh Kumar


I would use both CSS and JavaScript to accomplish this: http://jsfiddle.net/maximgladkov/XaMzB/1/

JavaScript

window.show = function(elementId) { 
    var elements = document.getElementsByTagName("div");
    for (var i = 0; i < elements.length; i++)
        elements[i].className = "hidden";

    document.getElementById(elementId).className = "";
}

CSS

.hidden {
    display: none;
}

HTML

<button type="button" onclick="show('id1');">Button 1</button>
<button type="button" onclick="show('id2');">Button 2</button>
<button type="button" onclick="show('id3');">Button 3</button>

<div id="id1">text 1</div>
<div id="id2" class="hidden">text 2</div>
<div id="id3" class="hidden">text 3</div>
like image 1
Maksim Gladkov Avatar answered Nov 19 '22 17:11

Maksim Gladkov