Here you will learn how to change the backgroundImage CSS property of div HTML <div> element using its onclick event. Javascript DOM document object consists of getElementById method that further provides style object. Javascript style object enables you to get or set the various CSS style properties dynamically. The Javascript style object provides the backgroundImage property of CSS style that can be used to change the background image of HTML <div> DIV elements or any other HTML element that supports this property. Pass the id attribute value of <div> tag whose backgroundImage you want to change dynamically.
document. elementById("id"). style. backgroundImage = "value";
style object and backgroundImage property both are case sensitive in Javascript so use the same pattern of letters to implement the code syntax.
In the above example there is a code for embedded CSS styles. background-image CSS property is used to set the background image of div tag. This property is accessed in Javascript code using style object.
Download the following images to try them with sample Javascript code:
blue.gif
green.gif
Be the first to rate this post
Tags: javascript, html, dom, css, javascript change backgroundimage, html div tag, free css tutorials, javascript document object, javascript getelementbyid, document object modeling, change div backgroundimage, dhtml, dynamic html, css background-image property
8/23/2008 8:56:47 PM