There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Web Design & Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Change CSS based on script results


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
drdrew1469's Avatar
Senior Member with 436 posts.
 
Join Date: Nov 2000
Location: PA
Experience: God
17-Aug-2007, 10:01 AM #1
Change CSS based on script results
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>
__________________
Please help me as I am attempting to (re)gain my (in)sanity.
drdrew1469's Avatar
Senior Member with 436 posts.
 
Join Date: Nov 2000
Location: PA
Experience: God
17-Aug-2007, 10:52 AM #2
my brain hurts today...the following code (1) obviously works but when trying to replace it with a variable (2) i'm getting syntax errors (anything, not what's shown, I know that's wrong, lol...d'oh!)

Code:
(1)  <body onLoad="document.getElementById(2).style.display = 'block'">

(2)  <body onLoad="document.getElementById("' + getWeekNr + '").style.display = 'block'">
tomdkat's Avatar
Computer Specs
Distinguished Member with 3,515 posts.
 
Join Date: May 2006
Location: S.F. Bay Area, CA
Experience: Intermediate
17-Aug-2007, 04:14 PM #3
Quote:
Originally Posted by drdrew1469
my brain hurts today...the following code (1) obviously works but when trying to replace it with a variable (2) i'm getting syntax errors (anything, not what's shown, I know that's wrong, lol...d'oh!)

Code:
(1)  <body onLoad="document.getElementById(2).style.display = 'block'">

(2)  <body onLoad="document.getElementById("' + getWeekNr + '").style.display = 'block'">
Based on the JavaScript in the first post, it looks like "getWeekNr" is a function, so "getWeekNr()" is how it should be coded, I'm thinking.

See what that does.

Peace...
drdrew1469's Avatar
Senior Member with 436 posts.
 
Join Date: Nov 2000
Location: PA
Experience: God
18-Aug-2007, 10:44 PM #4
tom, pretty sure i tried that...stupid syntax anyway, lol
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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 On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 10:48 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.