4 replies [Last post]
bottrill
Offline
newbie
Last seen: 15 years 37 weeks ago
Joined: 2005-02-02
Posts: 1
Points: 0

First of all , Hi.

I looking to load css files using keystrokes which i have manged to find on the web. However i am wanting to load a style sheet when a key is pressed. I.e. when 1 is presed it loads css1 etc. now i know i have to use javascript functions to load to the css file, which i have achieved using a simple set of buttons and also a form with <option> tags. The code for the working button loading css file looks as follows.

<SCRIPT LANGUAGE="JavaScript">

function LoadCSS(name) {
document.getElementById('style').href = name + '.css';
}

function LoadCSS2() {
document.getElementById('style').href = document.loadCSS.menu.options[document.loadCSS.menu.selectedIndex].value + '.css';
}
</SCRIPT>

<INPUT TYPE="button" VALUE="Load css1"
onClick="LoadCSS('york')">
<INPUT TYPE="button" VALUE="Load css2"
onClick="LoadCSS('york2')">

Now basically am wanting to know how i can load the same css files using keys 1 and 2 on the keyboard.

Any help much appreciated.

Thanks
Lee.

Tags:
Hugo
Hugo's picture
Offline
Moderator
London
Last seen: 5 years 41 weeks ago
London
Joined: 2004-06-06
Posts: 15668
Points: 2806

Loading CSS files

Hi bottrill,

This is really a JavaScript question , and is best moved out of the 'beginners CSS' area, so I'll move it to the 'Off Topic' area for you.
hopefully Tony or one of the others with better grasp of JavaScript than I have will be able to answer this for you .

Hugo.

Before you make your first post it is vital that you READ THE POSTING GUIDELINES!
----------------------------------------------------------------
Please post ALL your code - both CSS & HTML - in [code] tags
Please validate and ensure you have included a full Doctype before posting.
Why validate? Read Me

Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 1 week 2 days ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5343
Points: 2964

Keypress

Hi bottrill,
Hugo is right this really should be asked at a JavaScript forum or follow the http://www.javascriptkit.com/ link in the advertisement above.

But as Hugo knows I like a challenge and have put something together for you thanks to a great book Dynamic HTML by Danny Goodman.

function keypress(evt){ 
	var thekey; 
	evt=(evt)? evt: ((event)? event:null);  
	var charCode = (evt.charCode)?evt.charCode : evt.keyCode;  
	thekey=String.fromCharCode(charCode);  
	if(thekey >=1 && thekey <= 2){ 
		LoadCSS("york"+thekey); 
	} 
} 
document.onkeypress=keypress;

Hugo
Hugo's picture
Offline
Moderator
London
Last seen: 5 years 41 weeks ago
London
Joined: 2004-06-06
Posts: 15668
Points: 2806

Loading CSS files

I just knew that you would rise to the challenge Tony Smile just can't resist a JavaScript/DOM problem Wink

Before you make your first post it is vital that you READ THE POSTING GUIDELINES!
----------------------------------------------------------------
Please post ALL your code - both CSS & HTML - in [code] tags
Please validate and ensure you have included a full Doctype before posting.
Why validate? Read Me

Tony
Tony's picture
Offline
Moderator
Brisbane
Last seen: 1 week 2 days ago
Brisbane
Timezone: GMT+10
Joined: 2003-03-12
Posts: 5343
Points: 2964

Loading CSS files

Yes I like to dabble in the DOM Smile
Makes a good change from straight CSS questions.