I've got a simple html file with javascript controlling a 'toggle' function on divs (all set to style="display:none;"). I have another script that returns the current week number (which corresponds to each div id respectively). Everything works great, now I want to take it one step further. How can I, with the result of the getWeekNr script toggle that div to show by default? Snips below.
tia,
drew
Code:
<script language="JavaScript">
var ids=new Array('1','2','3');
function switchid(id){
hideallids();
showdiv(id);
}
function hideallids(){
//loop through the array and hide each element by id
for (var i=0;i<ids.length;i++){
hidediv(ids[i]);
}
}
function hidediv(id) {
//safe function to hide an element with a specified id
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById(id).style.display = 'none';
}
else {
if (document.layers) { // Netscape 4
document.id.display = 'none';
}
else { // IE 4
document.all.id.style.display = 'none';
}
}
}
function showdiv(id) {
//safe function to show an element with a specified id
if (document.getElementById) { // DOM3 = IE5, NS6
document.getElementById(id).style.display = 'block';
}
else {
if (document.layers) { // Netscape 4
document.id.display = 'block';
}
else { // IE 4
document.all.id.style.display = 'block';
}
}
}
</script>
<script language="JavaScript">
function getWeekNr()
{
var today = new Date();
Year = takeYear(today);
Month = today.getMonth();
Day = today.getDate();
now = Date.UTC(Year,Month,Day+1,0,0,0);
var Firstday = new Date();
Firstday.setYear(Year);
Firstday.setMonth(0);
Firstday.setDate(1);
then = Date.UTC(Year,0,1,0,0,0);
var Compensation = Firstday.getDay();
if (Compensation > 3) Compensation -= 4;
else Compensation += 3;
NumberOfWeek = Math.round((((now-then)/86400000)+Compensation)/7);
return NumberOfWeek;
}
function takeYear(theDate)
{
x = theDate.getYear();
var y = x % 100;
y += (y < 38) ? 2000 : 1900;
return y;
}
function init()
{
document.forms[0].weeknr.value=getWeekNr()
}
</script>
<a href="javascript:void()" onClick="switchid('1');return false">Week 1</a>
<a href="javascript:void()" onClick="switchid('2');return false">Week 2</a>
<a href="javascript:void()" onClick="switchid('3');return false">Week 3</a>
<div id="1" style="display:none;">stuff 1</div>
<div id="2" style="display:none;">stuff 2</div>
<div id="3" style="display:none;">stuff 3</div>