milesdriven
01-09-2012, 05:16 PM
I wrote a script that creates 2 absolutely positioned divs, in perfect alignment, one on top of the other. I want to use javascript to manipulate the display property of each div to show one div at a time by toggling each div's display property between none and block with an onClick event handler attached to the link.
This script works fine when the javascript function has no parameters, because the div id's are inserted directly into the getElementById function.
When I say it works, I mean the div with id=cal2 displays onload, then disappears when the Show Calendar link is clicked. The div that appears in its place has a link that does the reverse when clicked.
I want to create multiple pairs of overlapping divs that toggle their visibility, so I want to put parameters inside the functions that identify the specific pair of divs that need to be toggled per function call.
When I try to use variables/parameters in the functions to insert specific div id's into getElementById, it does not work.
PLEASE NOTE, the code below has div id's in green that don't have quotes of any kind. It still works in my firefox browser. I thought id's needed quotes, but it works without. I don't know why.
The code below works, but has no parameters in the functions:
<!DOCTYPE html>
<xhtml>
<head>
<title>Practice Calendar</title>
<style type="text/css">
.practicecalendar
{
position: absolute;
top: 0px;
left: 0px;
margin: 0;
width: 18em;
background: #B3FF99;
text-align: center;
}
.practiceschedule
{
position: absolute;
top: 0px;
left: 0px;
margin: 0;
width: 18em;
background: #99E6FF;
text-align: center;
}
</style>
<script type="text/javascript">
<!--
function showSchedule(){
document.getElementById('sch2').style.display = 'block';
document.getElementById('cal2').style.display = 'none';
}
function showCalendar(){
document.getElementById('cal2').style.display = 'block';
document.getElementById('sch2').style.display = 'none';
}
//-->
</script>
</head>
<body>
<div id=cal2 class='practicecalendar' style='
display: block;
'>
<p>Calendar</p>
<p><a href='' onClick='showSchedule(); return false'>Show Schedule</a></p>
</div>
<div id=sch2 class='practiceschedule' style='
display: none;
'>
<p>Schedule</p>
<p><a href='' onClick='showCalendar(); return false'>Show Calendar</a></p>
</div>
</body>
</xhtml>
When I add the information in red to the same script above, I get the script below that does not work.
<!DOCTYPE html>
<xhtml>
<head>
<title>Practice Calendar</title>
<style type="text/css">
.practicecalendar
{
position: absolute;
top: 0px;
left: 0px;
margin: 0;
width: 18em;
background: #B3FF99;
text-align: center;
}
.practiceschedule
{
position: absolute;
top: 0px;
left: 0px;
margin: 0;
width: 18em;
background: #99E6FF;
text-align: center;
}
</style>
<script type="text/javascript">
<!--
function showSchedule(sch,cal){
document.getElementById('sch').style.display = 'block';
document.getElementById('cal').style.display = 'none';
}
function showCalendar(cal,sch){
document.getElementById('cal').style.display = 'block';
document.getElementById('sch').style.display = 'none';
}
//-->
</script>
</head>
<body>
<div id=cal2 class='practicecalendar' style='
display: block;
'>
<p>Calendar</p>
<p><a href='' onClick='showSchedule('sch2','cal2'); return false'>Show Schedule</a></p>
</div>
<div id=sch2 class='practiceschedule' style='
display: none;
'>
<p>Schedule</p>
<p><a href='' onClick='showCalendar('cal2','sch2'); return false'>Show Calendar</a></p>
</div>
</body>
</xhtml>
When I remove the single quotes from the blue code, it does not work either.
<!DOCTYPE html>
<xhtml>
<head>
<title>Practice Calendar</title>
<style type="text/css">
.practicecalendar
{
position: absolute;
top: 0px;
left: 0px;
margin: 0;
width: 18em;
background: #B3FF99;
text-align: center;
}
.practiceschedule
{
position: absolute;
top: 0px;
left: 0px;
margin: 0;
width: 18em;
background: #99E6FF;
text-align: center;
}
</style>
<script type="text/javascript">
<!--
function showSchedule(sch,cal){
document.getElementById('sch').style.display = 'block';
document.getElementById('cal').style.display = 'none';
}
function showCalendar(cal,sch){
document.getElementById('cal').style.display = 'block';
document.getElementById('sch').style.display = 'none';
}
//-->
</script>
</head>
<body>
<div id=cal2 class='practicecalendar' style='
display: block;
'>
<p>Calendar</p>
<p><a href='' onClick='showSchedule(sch2,cal2); return false'>Show Schedule</a></p>
</div>
<div id=sch2 class='practiceschedule' style='
display: none;
'>
<p>Schedule</p>
<p><a href='' onClick='showCalendar(cal2,sch2); return false'>Show Calendar</a></p>
</div>
</body>
</xhtml>
Thanks for reading all of this. Does anyone know what I'm missing?
This script works fine when the javascript function has no parameters, because the div id's are inserted directly into the getElementById function.
When I say it works, I mean the div with id=cal2 displays onload, then disappears when the Show Calendar link is clicked. The div that appears in its place has a link that does the reverse when clicked.
I want to create multiple pairs of overlapping divs that toggle their visibility, so I want to put parameters inside the functions that identify the specific pair of divs that need to be toggled per function call.
When I try to use variables/parameters in the functions to insert specific div id's into getElementById, it does not work.
PLEASE NOTE, the code below has div id's in green that don't have quotes of any kind. It still works in my firefox browser. I thought id's needed quotes, but it works without. I don't know why.
The code below works, but has no parameters in the functions:
<!DOCTYPE html>
<xhtml>
<head>
<title>Practice Calendar</title>
<style type="text/css">
.practicecalendar
{
position: absolute;
top: 0px;
left: 0px;
margin: 0;
width: 18em;
background: #B3FF99;
text-align: center;
}
.practiceschedule
{
position: absolute;
top: 0px;
left: 0px;
margin: 0;
width: 18em;
background: #99E6FF;
text-align: center;
}
</style>
<script type="text/javascript">
<!--
function showSchedule(){
document.getElementById('sch2').style.display = 'block';
document.getElementById('cal2').style.display = 'none';
}
function showCalendar(){
document.getElementById('cal2').style.display = 'block';
document.getElementById('sch2').style.display = 'none';
}
//-->
</script>
</head>
<body>
<div id=cal2 class='practicecalendar' style='
display: block;
'>
<p>Calendar</p>
<p><a href='' onClick='showSchedule(); return false'>Show Schedule</a></p>
</div>
<div id=sch2 class='practiceschedule' style='
display: none;
'>
<p>Schedule</p>
<p><a href='' onClick='showCalendar(); return false'>Show Calendar</a></p>
</div>
</body>
</xhtml>
When I add the information in red to the same script above, I get the script below that does not work.
<!DOCTYPE html>
<xhtml>
<head>
<title>Practice Calendar</title>
<style type="text/css">
.practicecalendar
{
position: absolute;
top: 0px;
left: 0px;
margin: 0;
width: 18em;
background: #B3FF99;
text-align: center;
}
.practiceschedule
{
position: absolute;
top: 0px;
left: 0px;
margin: 0;
width: 18em;
background: #99E6FF;
text-align: center;
}
</style>
<script type="text/javascript">
<!--
function showSchedule(sch,cal){
document.getElementById('sch').style.display = 'block';
document.getElementById('cal').style.display = 'none';
}
function showCalendar(cal,sch){
document.getElementById('cal').style.display = 'block';
document.getElementById('sch').style.display = 'none';
}
//-->
</script>
</head>
<body>
<div id=cal2 class='practicecalendar' style='
display: block;
'>
<p>Calendar</p>
<p><a href='' onClick='showSchedule('sch2','cal2'); return false'>Show Schedule</a></p>
</div>
<div id=sch2 class='practiceschedule' style='
display: none;
'>
<p>Schedule</p>
<p><a href='' onClick='showCalendar('cal2','sch2'); return false'>Show Calendar</a></p>
</div>
</body>
</xhtml>
When I remove the single quotes from the blue code, it does not work either.
<!DOCTYPE html>
<xhtml>
<head>
<title>Practice Calendar</title>
<style type="text/css">
.practicecalendar
{
position: absolute;
top: 0px;
left: 0px;
margin: 0;
width: 18em;
background: #B3FF99;
text-align: center;
}
.practiceschedule
{
position: absolute;
top: 0px;
left: 0px;
margin: 0;
width: 18em;
background: #99E6FF;
text-align: center;
}
</style>
<script type="text/javascript">
<!--
function showSchedule(sch,cal){
document.getElementById('sch').style.display = 'block';
document.getElementById('cal').style.display = 'none';
}
function showCalendar(cal,sch){
document.getElementById('cal').style.display = 'block';
document.getElementById('sch').style.display = 'none';
}
//-->
</script>
</head>
<body>
<div id=cal2 class='practicecalendar' style='
display: block;
'>
<p>Calendar</p>
<p><a href='' onClick='showSchedule(sch2,cal2); return false'>Show Schedule</a></p>
</div>
<div id=sch2 class='practiceschedule' style='
display: none;
'>
<p>Schedule</p>
<p><a href='' onClick='showCalendar(cal2,sch2); return false'>Show Calendar</a></p>
</div>
</body>
</xhtml>
Thanks for reading all of this. Does anyone know what I'm missing?