Javascript Change Background Color Style

by top54u.com 25 Jun, 2008

Javascript DOM style object provides the backgroundColor property of CSS styles to change the background color of HTML tags. You can use the Javascript function to change the background color style of any HTML element that has some width and height so that it could reflect the change in color of its background. Javascript style object consists of all the CSS properties that can be accessed using Javascript DOM method. You can access the HTML document Elements using Javascript document object’s methods such as document.getElementById or document.getElementByName. By passing the Id or Name of the HTML element to these Javascript methods you can use the style object along with CSS property name to change the background color of that particular HTML element or tag.

 

Syntax of Javascript style object to change Background Color

 

document.getElementById("HTML element id"). style. backgroundColor = "value";

 

Javascript style object name and CSS property name are case sensitive. Use the same pattern as it is used in the above syntax.

 

Example of Javascript Change Background Color Style

 

<html>

<head>

<title>Javascript Change Background Color Style</title>

 

 

<script language="javascript" type="text/javascript">

function changeBackgroundColor(objDivID)

{

var backColor = new String();

backColor = document.getElementById(objDivID).style.backgroundColor;

if(backColor.toLowerCase()=='#eeeeee')

{

document.getElementById(objDivID).style.backgroundColor = '#c0c0c0';

}

else

{

document.getElementById(objDivID).style.backgroundColor = '#eeeeee';

}

}

</script> </head> <body>

<div id="div1" style="background-color : #EEEEEE">

Javascript will change the background color of this Div tag dynamically.

</div> <input type="button" value="click here" onclick="changeBackgroundColor('div1')" /> </body>

</html>

Spotlight.....

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , , , , , ,

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

10/11/2008 3:23:27 AM




related videos.....
recent posts.....
top54u ezines.....