/    Sign up×
Community /Pin to ProfileBookmark

Div Swap, only display one

I am looking for js to toggle divs on and off. i want only one div to display at a time. here is what i have:

[CODE]// Swap
var lastDiv = 0;

function swap(num) {
//show div
document.getElementById(“swap”+num).style.display = ‘block’;
//hide old div, set new old div
document.getElementById(“swap”+ lastDiv).style.display = ‘none’;
lastDiv = num;
}[/CODE]

and

[CODE]<div style=”cursor:pointer” onClick=”swap(0)”>Save</div>
<div style=”cursor:pointer” onClick=”swap(1)”>Edit</div>
<div style=”cursor:pointer” onClick=”swap(2)”>Delete</div>
<div style=”cursor:pointer” onClick=”swap(3)”>Add</div>

<div id=”swap0″ style=”display:none”>this is div0</div>
<div id=”swap1″ style=”display:none”>this is div1</div>
<div id=”swap2″ style=”display:none”>this is div2</div>
<div id=”swap3″ style=”display:none”>this is div3</div>[/CODE]

My problem is that i cant click the first link to dispay div 0, and when i click on div 1 to show, then to hide, i can’t make it display again.

Thanks!

to post a comment
JavaScript

15 Comments(s)

Copy linkTweet thisAlerts:
@_Aerospace_Eng_Oct 10.2006 — &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;style type="text/css"&gt;
div.button {
cursor:pointer;
}
&lt;/style&gt;
&lt;script type="text/javascript"&gt;
var num = 4; // this is # of divs you have
function swap(what)
{
for(var i = 0; i &lt; num; i++)
{
document.getElementById('swap'+i).style.display = 'none';
}
document.getElementById('swap'+what).style.display = 'block';
}
var createStyle = document.createElement('style');
var createStyleProp = document.createTextNode('.hidden { display:none; }');
createStyle.appendChild(createStyleProp);
document.getElementsByTagName('head')[0].appendChild(createStyle);
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class="button" onClick="swap(0)"&gt;Save&lt;/div&gt;
&lt;div class="button" onClick="swap(1)"&gt;Edit&lt;/div&gt;
&lt;div class="button" onClick="swap(2)"&gt;Delete&lt;/div&gt;
&lt;div class="button" onClick="swap(3)"&gt;Add&lt;/div&gt;
&lt;div id="swap0" class="hidden"&gt;this is div0&lt;/div&gt;
&lt;div id="swap1" class="hidden"&gt;this is div1&lt;/div&gt;
&lt;div id="swap2" class="hidden"&gt;this is div2&lt;/div&gt;
&lt;div id="swap3" class="hidden"&gt;this is div3&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@stevebor1authorOct 10.2006 — Thanks!

the .hidden class part in the js didnt work for me.... ill look into it.

also, what if i wanted to use this as a general js across the site. some pages have various amounts of divs, and i see you have to specify the amount in the js.
Copy linkTweet thisAlerts:
@forty2Oct 10.2006 — Look this or make with XMLHttpRequest();
<i>
</i>&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;title&gt;New Document&lt;/title&gt;
&lt;style type="text/css"&gt;
.button {
cursor:pointer;
}
#main {
border: 1px solid black;
width: 200px;
display: none;
}
&lt;/style&gt;
&lt;script type="text/javascript"&gt;
function ChangeContent(sender) {
var obj=document.getElementById('main');
obj.style.display='block';
obj.innerHTML=sender;
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class="button" onClick="ChangeContent('text 1')"&gt;Save&lt;/div&gt;
&lt;div class="button" onClick="ChangeContent('text 2')"&gt;Edit&lt;/div&gt;
&lt;div class="button" onClick="ChangeContent('text 3')"&gt;Delete&lt;/div&gt;
&lt;div class="button" onClick="ChangeContent('text 4')"&gt;Add&lt;/div&gt;
&lt;div id="main"&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@stevebor1authorOct 10.2006 — works kinda like the first one, but i am entering a lot of info in each div, so i dont wasnt to have to put all that code between the ' '. I like the first one, just need to make it not dependent on how many divs there are. i guess i could always make multiple js files, but that is the last resort ?
Copy linkTweet thisAlerts:
@_Aerospace_Eng_Oct 11.2006 — This is why you need to explain yourself better in the beginning. If you tell us only minimal details you likely don't get the answer you want. This will work for any number of divs on the page as long as they have ids in them that contain the word 'swap'. Only issue with this is the divs are seen briefly until the page is finished loading. I do this because the user may have JS disabled. If this is only intended for an admin area where you will controlling whether JS is enabled or not then you can make them all display:none in the beginning.
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;title&gt;Untitled Document&lt;/title&gt;
&lt;style type="text/css"&gt;
div.button {
cursor:pointer;
}
&lt;/style&gt;
&lt;script type="text/javascript"&gt;
function swap(what)
{
var theDivs = document.getElementsByTagName('div');
for(var i = 0; i &lt; theDivs.length; i++)
{
if(theDivs[i].id.search('swap') == 0)
{
theDivs[i].style.display = 'none';
}
}
document.getElementById('swap'+what).style.display = 'block';
}
window.onload = function()
{
var theDivs2 = document.getElementsByTagName('div');
for(var j = 0; j &lt; theDivs2.length; j++)
{
if(theDivs2[j].id.search('swap') != -1)
{
theDivs2[j].style.display = 'none';
}
}
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;div class="button" onClick="swap(0)"&gt;Save&lt;/div&gt;
&lt;div class="button" onClick="swap(1)"&gt;Edit&lt;/div&gt;
&lt;div class="button" onClick="swap(2)"&gt;Delete&lt;/div&gt;
&lt;div class="button" onClick="swap(3)"&gt;Add&lt;/div&gt;
&lt;div class="button" onClick="swap(4)"&gt;Save&lt;/div&gt;
&lt;div class="button" onClick="swap(5)"&gt;Edit&lt;/div&gt;
&lt;div class="button" onClick="swap(6)"&gt;Delete&lt;/div&gt;
&lt;div class="button" onClick="swap(7)"&gt;Add&lt;/div&gt;
&lt;div class="button" onClick="swap(8)"&gt;Save&lt;/div&gt;
&lt;div class="button" onClick="swap(9)"&gt;Edit&lt;/div&gt;
&lt;div class="button" onClick="swap(10)"&gt;Delete&lt;/div&gt;
&lt;div class="button" onClick="swap(11)"&gt;Add&lt;/div&gt;
&lt;div id="swap0"&gt;this is div0&lt;/div&gt;
&lt;div id="swap1"&gt;this is div1&lt;/div&gt;
&lt;div id="swap2"&gt;this is div2&lt;/div&gt;
&lt;div id="swap3"&gt;this is div3&lt;/div&gt;
&lt;div id="swap4"&gt;this is div4&lt;/div&gt;
&lt;div id="swap5"&gt;this is div5&lt;/div&gt;
&lt;div id="swap6"&gt;this is div6&lt;/div&gt;
&lt;div id="swap7"&gt;this is div7&lt;/div&gt;
&lt;div id="swap8"&gt;this is div8&lt;/div&gt;
&lt;div id="swap9"&gt;this is div9&lt;/div&gt;
&lt;div id="swap10"&gt;this is div10&lt;/div&gt;
&lt;div id="swap11"&gt;this is div11&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@stevebor1authorOct 11.2006 — Works great! thanks!. i didn't realize the div count till later. you are the css, i mean js master ?
Copy linkTweet thisAlerts:
@stevebor1authorOct 17.2006 — ok. one small modification...

the div flicker is too much onload. what do i need to do to NOT show the divs on page load?
Copy linkTweet thisAlerts:
@_Aerospace_Eng_Oct 17.2006 — Well you already had the divs doing that in the first example you posted. Up to you to figure it out again. I told you that this would happen and I told you what to do as well.
Copy linkTweet thisAlerts:
@stevebor1authorOct 17.2006 — I did set all the divs to display:none in the css; and it still flashes. sorry to bother, im not a js guy.
Copy linkTweet thisAlerts:
@_Aerospace_Eng_Oct 17.2006 — Post your updated code, the code where you said you made them display:none. From the sounds of it you didn't do it correctly.
Copy linkTweet thisAlerts:
@stevebor1authorOct 17.2006 — i had one div set to "block" so maybe that was causing the extra glitch. so it would load block then switch to none.... I will test and verify.
Copy linkTweet thisAlerts:
@stevebor1authorJan 06.2007 — if i wanted to now be able to link to this page and call up one of the div, how would i do that?

http://mysite.com/page.jsp?tab=something ......
Copy linkTweet thisAlerts:
@yawnDec 09.2009 — I know I'm resurrecting a very old thread, but I'm dealing with this exact same issue. Unfortunately, I know basically nothing about Javascript, so I was wondering if _Aerospace_Eng_ or someone else would be able to help me out. I took the code that _Aerospace_Eng_ provided the second time and everything worked out great, except the fact that all of the divs are shown. I'm not sure where to insert "display:none" in the code so that only the first div shows when the page loads. Here is a link to the page I'm building: http://homepages.uwp.edu/macdo019/artists/artists.html. And apparently my code is too long to post all together, so I'm going to split it across two. Thanks in advance to anyone who can help.

[CODE]<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Deckle</title>
<style type="text/css">
<!--
body {
text-align: center;
margin-top: 0px;
background-image: url(../images/bg.png);
}
#wrapper {
width: 850px;
position: relative;
margin-right: auto;
margin-left: auto;
background-color: #FFF;
height: 1200px;
}
#header {
height: 150px;
width: 850px;
position: absolute;
top: 0px;
background-image: url(../images/header/header.png);
}
#menu1 {
height: 95px;
width: 70px;
position: absolute;
top: 28px;
right: 138px;
border-right-width: thin;
border-right-style: dotted;
border-right-color: #333;
}
#menu2 {
height: 100px;
width: 70px;
position: absolute;
top: 28px;
right: 45px;
}
#logo {
height: 92px;
width: 217px;
position: absolute;
left: 38px;
top: 28px;
}
#heading1 {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
height: 20px;
width: 605px;
position: absolute;
top: 175px;
text-align: left;
padding-top: 5px;
padding-left: 5px;
background-image: url(../images/headings/headingbkg.png);
color: #F3F2EC;
letter-spacing: 0.2em;
font-weight: bold;
left: 5px;
}
#divider {
height: 985px;
width: 4px;
position: absolute;
top: 210px;
right: 233px;
background-image: url(../images/artists/line.png);
}
#heading2 {
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
height: 20px;
width: 215px;
position: absolute;
top: 175px;
text-align: left;
padding-top: 5px;
padding-left: 5px;
background-image: url(../images/headings/headingbkg.png);
color: #F3F2EC;
letter-spacing: 0.2em;
font-weight: bold;
right: 5px;
}
#gallery1 {
height: 225px;
width: 450px;
position: absolute;
left: 5px;
top: 210px;
}
#thumb1 {
height: 50px;
width: 130px;
position: absolute;
top: 210px;
left: 455px;
}
#thumb2 {
height: 50px;
width: 130px;
position: absolute;
top: 268px;
left: 455px;
}
#thumb3 {
height: 50px;
width: 130px;
position: absolute;
top: 326px;
left: 455px;
}
#thumb4 {
height: 50px;
width: 130px;
position: absolute;
top: 385px;
left: 455px;
}
#search {
height: 72px;
width: 220px;
position: absolute;
top: 210px;
right: 5px;
text-align: left;
background-image: url(../images/login/formbg.png);
}
-->
</style>
<script src="../SpryAssets/SpryMenuBar.js" type="text/javascript"></script>
<script src="file:///Macintosh HD/Users/macdo019/Desktop/SpryAssets/SpryValidationCheckbox.js"
type="text/javascript"></script>
<link href="../SpryAssets/SpryMenuBarVertical.css" rel="stylesheet" type="text/css" />
<style type="text/css">
<!--
a:link {
text-decoration: none;
color: #000;
}
a:visited {
text-decoration: none;
color: #951925;
}
a:hover {
text-decoration: none;
color: #951925;
}
a:active {
text-decoration: none;
color: #951925;
}
-->
</style>
<link href="../exss/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript">
<!--
function swap(what)
{
var theDivs = document.getElementsByTagName('div');
for(var i = 0; i < theDivs.length; i++)
{
if(theDivs[i].id.search('swap') == 0)
{
theDivs[i].style.display = 'none';
}
}
document.getElementById('swap'+what).style.display = 'block';
}
window.onload = function()
{
var theDivs2 = document.getElementsByTagName('div');
for(var j = 0; j < theDivs2.length; j++)
{
if(theDivs2[j].id.search('swap') != -1)
{
theDivs2[j].style.display = 'none';
}
}
}
function MM_preloadImages() { //v3.0
var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_openBrWindow(theURL,winName,features) { //v2.0
window.open(theURL,winName,features);
}
//-->
</script></head>

<body onload="MM_preloadImages('../images/artists/borlaug/ubt2.png','../images/artists/borlaug/ubt3.png','../images/artists/borlaug/ubt4.png','../images/artists/borlaug/sbt1.png','../images/artists/borlaug/ubt1.png','../images/artists/borlaug/sbt2.png','../images/artists/borlaug/sbt3.png','../images/artists/borlaug/sbt4.png')">
<div class="h1" id="wrapper">
<div id="header">
<div id="logo"><a href="../index.html"><img src="../images/logo/logo.png" alt="logo" name="deckle" width="217" height="92" border="0" id="deckle" /></a></div>
<div id="menu1">
<ul id="MenuBar1" class="MenuBarVertical">
<li><a href="../login/login.html">Log In</a></li>
<li><a href="../register/register.html">Register</a></li>
<li><a href="../aboutus/aboutus.html">About Us</a></li>
<li><a href="../news/news.html">News</a></li>
</ul>
</div>
<div id="menu2">
<ul id="MenuBar2" class="MenuBarVertical">
<li><a href="#">Artists</a></li>
<li><a href="#">Exhibitions</a></li>
<li><a href="#">Tutorials</a></li>
<li><a href="#">Contact Us</a></li>
</ul>
</div>
</div>
<div id="search">
<form id="form3" name="form2" method="post" action="">
<table width="221" height="72" border="0" cellpadding="0" cellspacing="5">
<tr>
<td width="65" height="30"><label>
<select name="type" class="body" id="type">
<option value="1">Artist</option>
<option value="2">Title</option>
<option value="3">Tag</option>
</select>
</label></td>
<td colspan="2"><input name="keyword" type="text" class="body" id="keyword" size="20" /></td>
</tr>
<tr>
<td height="24">&nbsp;</td>
<td width="102" height="24">&nbsp;</td>
<td width="83"><input name="search" type="submit" class="button" id="search2" value="Search" /></td>
</tr>
</table>
</form>
</div>
[/CODE]
Copy linkTweet thisAlerts:
@yawnDec 09.2009 — [CODE] <div class="h1" id="heading1">FEATURED ARTIST</div>
<div>Content for New Div Tag Goes Here</div>
<div id="gallery1"><span style="cursor:pointer">
<div id="swap0"><img src="../images/artists/borlaug/ub1.png" alt="ub1" name="B1" width="450" height="225" id="B1" onclick="MM_openBrWindow('../images/artists/borlaug/b1.png','','width=815,height=600')" /></div>
<div id="swap1"><img src="../images/artists/borlaug/ub2.png" alt="ub1" name="B1" width="450" height="225" id="B1" onclick="MM_openBrWindow('../images/artists/borlaug/b2.png','','width=600,height=812')" /></div>
<div id="swap2"><img src="../images/artists/borlaug/ub3.png" alt="ub1" name="B1" width="450" height="225" id="B1" onclick="MM_openBrWindow('../images/artists/borlaug/b3.png','','width=500,height=845')" /></div>
<div id="swap3"><img src="../images/artists/borlaug/ub4.png" alt="ub1" name="B1" width="450" height="225" id="B1" onclick="MM_openBrWindow('../images/artists/borlaug/b4.png','','width=630,height=800')" /></div>
</span></div>
<div id="thumb1"><div class="thumb" onClick="swap(0)"><img src="../images/artists/borlaug/sbt1.png" alt="sbt1" name="b1" width="130" height="50" id="b1" onclick="MM_swapImage('b1','','../images/artists/borlaug/sbt1.png','b2','','../images/artists/borlaug/ubt2.png','b3','','../images/artists/borlaug/ubt3.png','b4','','../images/artists/borlaug/ubt4.png',1)" /></div></div>
<div id="thumb2"><div class="thumb" onClick="swap(1)"><img src="../images/artists/borlaug/ubt2.png" alt="ubt1" name="b2" width="130" height="50" id="b2" onclick="MM_swapImage('b1','','../images/artists/borlaug/ubt1.png','b2','','../images/artists/borlaug/sbt2.png','b3','','../images/artists/borlaug/ubt3.png','b4','','../images/artists/borlaug/ubt4.png',1)" /></div></div>
<div id="thumb3"><div class="thumb" onClick="swap(2)"><img src="../images/artists/borlaug/ubt3.png" alt="ubt3" name="b3" width="130" height="50" id="b3" onclick="MM_swapImage('b1','','../images/artists/borlaug/ubt1.png','b2','','../images/artists/borlaug/ubt2.png','b3','','../images/artists/borlaug/sbt3.png','b4','','../images/artists/borlaug/ubt4.png',1)" /></div></div>
<div id="thumb4"><div class="thumb" onClick="swap(3)"><img src="../images/artists/borlaug/ubt4.png" alt="ubt4" name="b4" width="130" height="50" id="b4" onclick="MM_swapImage('b1','','../images/artists/borlaug/ubt1.png','b2','','../images/artists/borlaug/ubt2.png','b3','','../images/artists/borlaug/ubt3.png','b4','','../images/artists/borlaug/sbt4.png',1)" /></div></div>
<div id="divider"></div>
<div></div>
<div id="form">
<form id="form1" name="form1" method="post" action="">
</form>
</div>
<div class="body" id="form2"></div>
<div class="h1" id="heading2">SEARCH</div>
<div class="subheading" id="previous">
<table width="115" height="40" border="0">
<tr>
<td width="109" align="center" valign="middle" style="text-align: center"><a href="#" class="subheading">&lt;&lt;&lt;Older Articles</a></td>
</tr>
</table>
</div>
<div class="h1" id="heading3">NEWS</div>
<div class="h1" id="heading4">LOG IN</div>
</div>
<script type="text/javascript">
<!--
var MenuBar1 = new Spry.Widget.MenuBar("MenuBar1", {imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
var MenuBar2 = new Spry.Widget.MenuBar("MenuBar2", {imgRight:"SpryAssets/SpryMenuBarRightHover.gif"});
//-->
</script>
</body>
</html>[/CODE]
Copy linkTweet thisAlerts:
@yawnDec 10.2009 — I don't know if it's possible to edit posts here, but I no longer need any assistance, as I decided to go another route.
×

Success!

Help @stevebor1 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...