473,385 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Toggle divs with javascript

I never have luck in toggling divs with javascript, to which I am new.
Not even with a single browser, let alone making it compatible with all
major browsers. Also, I lack experience with client side codes.

I am gonna have a static calendar page like so:

2005
Jan
Feb
...
Dec

2006
Jan
Feb
...
Dec

2007
Jan
Feb
...
Dec

Initially, I only wanna display

2005
2006
2007

1. When 2005 is clicked I show the list of months below it.
2. When 2005 is clicked again, I hide the list of months of 2005.
3. Suppose, the list of months of 2005 is displayed right now, and the
user clicks on 2006, then hide the list of months of 2005, display the
list of months for 2006.
4. The same pattern happens with 2007.
5. The year headings are always displayed, but at any point of time,
only one list of months is displayed. They are mutually exclusive.

Simple (or maybe hard) as such, and below is what I have tried, but it
just refuses to work. Therefore, I am trying my luck here in this
forum.

<html>
<head>
<script language="javascript" type="text/javascript">
<!-- Begin

function toggleLayer(div1, div2, div3)
{
if (document.getElementById)
{
// this is the way the standards work
var style1 = document.getElementById(div1).style;
var style2 = document.getElementById(div2).style;
var style3 = document.getElementById(div3).style;
style1.display = "block";
style2.display = "none";
style3.display = "none";
}
else if (document.all)
{
// this is the way old msie versions work
var style1 = document.all[div1].style;
var style2 = document.all[div2].style;
var style3 = document.all[div3].style;
style1.display = "block";
style2.display = "none";
style3.display = "none";
}
else if (document.layers)
{
// this is the way nn4 works
var style1 = document.layers[div1].style;
var style2 = document.layers[div2].style;
var style3 = document.layers[div3].style;
style1.display = "block";
style2.display = "none
style3.display = "none";
}
}
// End -->
</script>
<titleJust a test</title>
</head>
<body>
<a href="#" onclick="toggleLayer('2005', '2006', '2007'); return
false;">2005</a><br/>
<div id="2005" style="display:none">
Jan<br/>
Feb<br/>
Mar<br/>s
Apr<br/>
May<br/>
Jun<br/>
Jul<br/>
Aug<br/>
Sep<br/>
Oct<br/>
Nov<br/>
Dec<br/>
</div>
<a href="#" onclick= "toggleLayer('2006', '2005', '2007'); return
false;">2006</a><br/>
<div id="2006" style="display:none">
Jan<br/>
Feb<br/>
Mar<br/>
Apr<br/>
May<br/>
Jun<br/>
Jul<br/>
Aug<br/>
Sep<br/>
Oct<br/>
Nov<br/>
Dec<br/>
</div>
<a href="#" onclick= "toggleLayer('2007', '2005', '2007'); return
false;">2007</a><br/>
<div id="2007" style="display:none">
Jan<br/>
Feb<br/>
Mar<br/>
Apr<br/>
May<br/>
Jun<br/>
Jul<br/>
Aug<br/>
Sep<br/>
Oct<br/>
Nov<br/>
Dec<br/>
</div>
</body>
</html>

Nov 7 '06 #1
1 3479
an***********@yahoo.com said the following on 11/7/2006 12:43 PM:
I never have luck in toggling divs with javascript, to which I am new.
Not even with a single browser, let alone making it compatible with all
major browsers. Also, I lack experience with client side codes.
<URL:
http://groups-beta.google.com/group/comp.lang.javascript/browse_thread/thread/487563540933112b/32d5aee6b6a49f5d?lnk=gst&q=Randy+Webb+changevisibi lity&rnum=3#32d5aee6b6a49f5d>

Is a thread with two functions:

changeVisibility and changeDisplay and some test code that you can run
to see how visibility/display work in the browser.
Simple (or maybe hard) as such, and below is what I have tried, but it
just refuses to work. Therefore, I am trying my luck here in this
forum.
<snip>
<div id="2005" style="display:none">
The ID Attribute can't start with a number.

There are some other issues with your code such as type attribute versus
language, the HTML comment delimiters, NN4 code, but you do get a kudo
for at least using a semi feature detection but a test for
getElementById isn't sufficient to tell if the browser can script the
style object.

Read the thread above, with it's functions, tinker some more and post if
you can't make it work.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 7 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jonathan | last post by:
Hi, I've read through quite a number of postings on here so far and have seen what look like very simply, reasonable answers to this question, however I am still completely unable to do what I...
2
by: leegold2 | last post by:
I wondered if anyone would give me code- I think it would be easy, but I'm a complete newbie. What I want to do is to show many tables in a brief truncated format and then for each table offer the...
2
by: Kingo | last post by:
Hi, First, please forgive my terrible knowledge of JS! I haven't used it in years. I am trying to create a Help page where a new question is on each line, and when clicking on the question,...
2
by: hzgt9b | last post by:
Check out the tree view on this page: http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.tr... When you hover over the border between the two frames, a tooltip displays:...
1
Markus
by: Markus | last post by:
I've made the peice of code below to toggle the selected elements 'display' from 'display' to 'none' and vice versa. Now i'd like to know if it's possible for me to toggle the display elements of 2...
11
by: dusk | last post by:
Hi, I'm very new to javascript, and I'm having trouble finding a way to display a string of divs without the code getting really messy. I have several different pages to write, each with about...
2
by: dusk | last post by:
Hi, I have a page with lots of hidden divs which are revealed based on choices made at each 'layer'. So I've used naming convention which represents the order in which each div becomes...
5
by: Mike P | last post by:
How would I show or hide a div that is using client side Javascript based upon a server side variable? Here are my divs : <div id="idButton5" class="otherLeftBarLink" onmouseover="javascript:...
4
by: ameshkin | last post by:
I have a checkbox with an ID of svc_tp_1, and an image that corresponds with this checkbox below it. <input type="checkbox" name="checkbox" id="svc_tp_1" value="svc_tp_1" / <img...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.