Changing a Element's Style with Javascript

This page shows a example of how to change a element's style with javascript. Press the following buttons to change the background color.

Make the above paragraph: RED YELLOW. (Click on it)

The javascript code looks like this:

var o = document.getElementById("t");

function f1() {o.style.backgroundColor="red";}
function f2() {o.style.backgroundColor="yellow";}

document.getElementById("b1").onclick = f1;
document.getElementById("b2").onclick = f2;

This technique can be used to change the css value for any html element. All you need to do is to give the html tag a “id”, and define in js when should the change happen.

Was this page useful? If so, please do donate $3, thank you donors!
Home
Terms of Use
About
Advertise
Subscribe
Google
2006-02
© 2006 by Xah Lee.