Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change CSS style on click 1

Status
Not open for further replies.

Bravogolf

Programmer
Nov 29, 2002
204
GB
Morning all (or afternoon, as the case may be!).

I'm almost finished a website I am working on. Everything is styled via the external CSS file.

However, I realised that it would be very handy to be able to change the colour scheme on the site to something else (like to green, or blue).

Does anyone know how to do this?

I had tried having a form, and the user selects the colour (from three options), and then according to the colour, uses the document.write function to create a new style....cant seem to get it though??

Any help at all is very much appreciated :)
 
This will do the trick:
Code:
<html>
<head>
	<link id="external_style" rel="stylesheet" type="text/css">
</head>
<body>
<script language="javascript">
function setStyle( oSel )
{	sCSS = oSel.value;
	if (sCSS!= "")	document.getElementById("external_style").href = sCSS;
}
</script>
<form>
<select name="style" onchange="setStyle(this)">
	<option value="">-- Choose style --</option>
	<option value="red.css">Red</option>
	<option value="green.css">Green</option>
	<option value="blue.css">Blue</option>
</select>
</form>
<hr>
Blah blah blah

</body>
</html>
 
Excellent, a lot tidier than my convuleted code :)

Thank you kindly.
 
Thanks again, VonGrunt for your help.
One more question, if I may?

How do I get it to remember what style was selected, so that when I change the page, the same chosen style sheet is in effect?

I was considering the use of cookies for this, but alas, I dont know how ...

Thanks again :-D
 
Yes, either cookies or frames. In both cases the answer is pure javascript, so please post question there.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top