|
Jan's Web Scripting Demo:
|
CSS name | JavaScript name |
font-size | fontSize |
background-color | backgroundColor |
border-width | borderWidth |
text-align | textAlign |
Try it: Move your mouse over each of the colors in the list or tap the color on a touchscreen. The Example DIV changes color.
To accomplish this task you need three things:
ID for DIV element
<div id="examplediv">
The original color for the DIV is set in the STYLE section in the HEAD of the page's source. It could be set in an inline STYLE in the tag.
Tip: It often seems to work better to set a specific value for any property that you plan to change with a script, instead of letting the browser use the default value for the property.
Script to change the background color for the DIV
<script>
function changecolor(mycolor)
{document.getElementById("examplediv").style.backgroundColor=
mycolor;
}
</script>
In the script the term mycolor stands in for the actual color you write when the function is called. Writing changecolor('red') would apply the color red as the background.
<ul>
<li><span onMouseOver="changecolor('red')">Red</span></li>
<li><span onMouseOver="changecolor('maroon')">Maroon</span></li>
<li><span onMouseOver="changecolor('yellow')">Yellow</span></li>
<li><span onMouseOver="changecolor('green')">Green</span></li>
<li><span onMouseOver="changecolor('blue')">Blue</span></li>
<li><span onMouseOver="changecolor('orange')"></span>Orange</li>
</ul>
Tip: To return the background color to the original, instead of a function attached to the onClick attribute of the Reset button, you could call the function from the matching onMouseOut event for each onMouseOver.
Tip: The example script puts the text inside a SPAN tag to keep the hotspot from extending all the way across the page.
This same method can be used to access any style property and change its value. Just watch your spelling and capitalization. Javascript will NOT try to figure out what you meant to say!