Properti style.backgroundColor digunakan untuk menetapkan warna tertentu latarbelakang suatu elemen.
Sintaks:
obj.style.backgroundColor = value;
Nilainya:
- color
Hex color, RGB color, Color name transparent
inherit
Contoh 1:
<HTML>
<HEAD>
<TITLE>style.backgroundColor</TITLE>
<SCRIPT TYPE="text/javascript">
function changeBgColor()
{
var list = document.forms[0].colorList;
document.body.style.backgroundColor = list.options[list.selectedIndex].value;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<SELECT NAME="colorList" onChange="changeBgColor()">
<OPTION SELECTED VALUE="white">White
<OPTION VALUE="blue">Blue
<OPTION VALUE="red">Red
<OPTION VALUE="green">Green
<OPTION VALUE="yellow">Yellow
<OPTION VALUE="black">Black
</SELECT>
</FORM>
</BODY>
</HTML>
Contoh 2:
<HTML>
<HEAD>
<TITLE>style.backgroundColor</TITLE>
<SCRIPT TYPE="text/javascript">
function changeBgColor()
{
var list = document.forms[0].colorList;
list.style.backgroundColor = list.options[list.selectedIndex].value;
}
</SCRIPT>
</HEAD>
<BODY>
<FORM>
<SELECT NAME="colorList" onChange="changeBgColor()">
<OPTION SELECTED VALUE="white">White
<OPTION VALUE="blue">Blue
<OPTION VALUE="red">Red
<OPTION VALUE="green">Green
<OPTION VALUE="yellow">Yellow
<OPTION VALUE="black">Black
</SELECT>
</FORM>
</BODY>
</HTML>