Change background colour with javascript
This tutorial shows you how to make a dropdown menu that changes the background colour of your page
Step1.
Create the dropdown menu that will be used to select a colour:
Step2.
Next we need a javascript function that will change the colour. Add the following code between the 'head' tags of the script:
change bgColor to fgColor to change font colour instead of background
Step3.
And finally we add a call to the javascript function in the 'select' tag as follows:
Notice how we assign a value to each option tag using either javascript named colours (red,blue) or hexadecimal colours (#ffffff).
We use onChange to call our function changeCol when a selection is made in the dropdown. The value of the selection is passed to the function using this.value
The complete code:
