Reply
JS display mutiple DIVs
Old 05-09-2008, 10:11 PM JS display mutiple DIVs
Skilled Talker

Posts: 64
Trades: 0
Hey, right now I'm using a very handy script which allows me to display different divs:

HTML Code:
<html>
<head>
<script type="text/javascript">
var current = "1";
function displaydiv(id){
	if(!document.getElementById) return false;
	var div = document.getElementById("page"+id);
	var curDiv = document.getElementById("page"+current);
	curDiv.style.display = "none";
	div.style.display = "block";
	current = id;
}
</script>
</head>
<body>
<input type="button" value="div1" onclick="displaydiv(1); return false;">
<input type="button" value="div2" onclick="displaydiv(2); return false;">

<div id="type">
<div id="page1">Hello</div>
<div id="page2">Goodbye</div>
<div id="page3">Good Day</div>
</div>

<script type="text/javascript">
var divs = document.getElementById("type").getElementsByTagName("div");
for(i = 0; i < divs.length; i++){divs[i].style.display = "none";}
document.getElementById("page1").style.display = "block";
</script>
</body>
</html>
My question is, is there a way that I could get a single button to open say...Divs 1 + 3 at the same time?
Petsmacker is offline
Reply With Quote
View Public Profile
 
 
When You Register, These Ads Go Away!
Old 05-10-2008, 01:45 AM Re: JS display mutiple DIVs
Average Talker

Posts: 18
Name: TK
Trades: 0
Here's one (kind of boring) way to it.
HTML Code:
<html>
<head>
<script type="text/javascript">
function displaydiv(sList){
    if(!document.getElementById) return false;
    
    //set all to off
    clearAll();
    //turn some back on
    sList += "";
    var ids=sList.split(/\s*,\s*/);
    for (id=0; id<ids.length; id++) {
        div = document.getElementById("page"+ids[id]);
        div.style.display = "block";
    }
}
function clearAll() {
    var divs = document.getElementById("type").getElementsByTagName("div");
    for(i = 0; i < divs.length; i++){divs[i].style.display = "none";}
}
</script>
</head>
<body>
<input type="button" value="div1" onclick="displaydiv('1'); return false;">
<input type="button" value="div2" onclick="displaydiv('2'); return false;">
<input type="button" value="div 1 and 3" onclick="displaydiv('1,3'); return false;">
<div id="type">
<div id="page1">Hello</div>
<div id="page2">Goodbye</div>
<div id="page3">Good Day</div>
</div>

<script type="text/javascript">
displaydiv("1");  //say hello
</script>
</body>
</html>

Last edited by zxcvbnm60; 05-10-2008 at 01:47 AM..
zxcvbnm60 is offline
Reply With Quote
View Public Profile
 
Old 05-10-2008, 01:52 AM Re: JS display mutiple DIVs
Skilled Talker

Posts: 64
Trades: 0
Inspired. Thank you very much its exactly what I was looking for. Cheers.
Petsmacker is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to JS display mutiple DIVs
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML

 


Page generated in 0.08817 seconds with 13 queries