PDA

View Full Version : Toggling an Element BG



abcdefGARY
05-16-2007, 03:17 AM
Hello, I found this useful javascript snippet that helps me toggle the background color of an element:


<script type="text/javascript">
function toggleBgColor( elem )
{
var style2 = elem.style;
style2.backgroundColor = style2.backgroundColor? "":"#FFFF00";
}
</script>

with this...


<div onclick="javascript:toggleBgColor( this );">testing</div>
<div id="testing-element">change this color</div>

the script can only change the bg-color of the current div (the "testing" text), however, I'd like to change the bg-color of a specific div element (for example, the "testing-element").

I think I need to add "GetElementbyID" or whatever to the script/code, but I'm not an expert on this field. any help would be greatful.

thanks.

tech_support
05-16-2007, 06:27 AM
<div onclick="javascript:toggleBgColor(document.getElementById('testing-element'));">testing</div>
<div id="testing-element">change this color</div>

abcdefGARY
05-16-2007, 06:38 AM
<div onclick="javascript:toggleBgColor(document.getElementById('testing-element'));">testing</div>
<div id="testing-element">change this color</div>wow, thanks. that was quick. :)