PDA

View Full Version : change background color


jorgenfi
05-14-2009, 11:34 PM
I try to learn how to change css background-properties via javascript, but i don't even get this simple one to work. What is wrong?

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript">

function changeBackground() {
document.getElementsByTagName("body").style.background="#00FF00";

}
</script>

</head>

<body>
blablabla<br>

<input type="button" onclick="changeBackground()" value="Change color" />

</body>
</html>

adios
05-14-2009, 11:39 PM
Before you use it - look up 'getElementsByTagName' :

Actually there is a better way because the standard DOM allows you to create your own collection of type of tag that appears on your web page. To do this we just replace document.getElementById with document.getElementsByTagName. Instead of obtaining a single object from our page that has the id that we specified we now get back an array of all of the objects that are identified by a particular tag.

document.getElementsByTagName("body")[0].style.background="#00FF00";

http://javascript.about.com/library/bldom07.htm

jorgenfi
05-14-2009, 11:42 PM
Thanks a lot! That made it work!!! :)

tyre
05-15-2009, 09:27 AM
http://www.trade9shoes.com