The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Changing CSS with option boxes
ajc39
post Dec 8 2009, 08:58 AM
Post #1





Group: Members
Posts: 2
Joined: 8-December 09
Member No.: 10,527



Hi, im a bit of a beginner when it comes to web design, my problem is that i want to be able to change css values of the website so that the background will change colour. At the moment I have intended to use a option drop down box and have the following code which unfortunatly I have not got to work

function changeCSS(sclassName)
{
document.getElementById('mainContent').className=sclassName;
document.getElementById('menu').className=sclassName;
document.getElementById('body').className=sclassName;
document.getElementById('*').className=sclassName;
}
<SELECT NAME="Favorite_Color2" SIZE="1" onchange="('Index: ' + this.selectedIndex
+ '\nValue: ' + this.options[this.selectedIndex].value)">
<OPTION SELECTED VALUE = "changeCSS('White');">White
<OPTION SELECTED VALUE = "changeCSS('Red');">Red
<OPTION SELECTED VALUE = "changeCSS('Blue');">Blue
<OPTION SELECTED VALUE = "changeCSS('Green');">Green
<OPTION SELECTED VALUE = "changeCSS('Yellow');">Yellow
<OPTION SELECTED VALUE = "changeCSS('Orange');">Orange
<OPTION SELECTED VALUE = "changeCSS('Purple');">Purple
</SELECT>

If anyone has a solution has to how to get the code to work, or perhaps an alternative way of doing this which may be easier would be highly greatfull.

Many thanks
Andy
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Rocco Tripaldi
post Dec 8 2009, 10:56 AM
Post #2





Group: Members
Posts: 2
Joined: 8-December 09
From: Portland, Maine
Member No.: 10,529



Try this:

<SELECT NAME="Favorite_Color2" SIZE="1" onchange="changeCSS(this.value)">
<OPTION SELECTED VALUE = "value">White</option>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
ajc39
post Dec 8 2009, 11:08 AM
Post #3





Group: Members
Posts: 2
Joined: 8-December 09
Member No.: 10,527



thanks for the quick reply, unfortunately this does not seem to do anything
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Dec 8 2009, 03:51 PM
Post #4


.
********

Group: WDG Moderators
Posts: 9,656
Joined: 10-August 06
Member No.: 7



QUOTE(ajc39 @ Dec 8 2009, 02:58 PM) *

document.getElementById('*').className=sclassName;

An ID value can't be "*". Is the purpose to give all elements a className? unsure.gif

QUOTE
<SELECT NAME="Favorite_Color2" SIZE="1" onchange="('Index: ' + this.selectedIndex + '\nValue: ' + this.options[this.selectedIndex].value)">

You must call the function changeCSS somewhere (e.g. in the onchange event). Also the function call's parameter must contain only the selected OPTION element's value. Something like:

CODE
<select onchange="changeCSS(this.options[this.selectedIndex].value);">


QUOTE
<OPTION SELECTED VALUE = "changeCSS('White');">White
<OPTION SELECTED VALUE = "changeCSS('Red');">Red
<OPTION SELECTED VALUE = "changeCSS('Blue');">Blue
<OPTION SELECTED VALUE = "changeCSS('Green');">Green
<OPTION SELECTED VALUE = "changeCSS('Yellow');">Yellow
<OPTION SELECTED VALUE = "changeCSS('Orange');">Orange
<OPTION SELECTED VALUE = "changeCSS('Purple');">Purple

You can't call the function changeCSS from the OPTION's value, instead the value becomes literally e.g. "changeCSS('White');" which you don't want. Try something like this instead (and make sure the style sheet classes match):

CODE
<option value="White">White
...etc.

Also you can only let one OPTION be pre-SELECTED (unless the MULTIPLE attribute is used).

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 24th April 2024 - 06:24 AM