Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

JavaScript Forum


You are currently viewing our JavaScript Forum as a guest. Please register to participate.
Login



Reply
JS display mutiple DIVs
Old 05-09-2008, 11: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
 
 
Register now for full access!
Old 05-10-2008, 02: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 02:47 AM..
zxcvbnm60 is offline
Reply With Quote
View Public Profile
 
Old 05-10-2008, 02: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.11649 seconds with 12 queries