Results 1 to 3 of 3

Thread: Toggling an Element BG

  1. #1
    Join Date
    Aug 2006
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Toggling an Element BG

    Hello, I found this useful javascript snippet that helps me toggle the background color of an element:

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

    Code:
    <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.

  2. #2
    Join Date
    May 2006
    Location
    Sydney, Australia - Near the coast.
    Posts
    1,995
    Thanks
    0
    Thanked 8 Times in 7 Posts

    Default

    Code:
    <div onclick="javascript:toggleBgColor(document.getElementById('testing-element'));">testing</div>
    <div id="testing-element">change this color</div>
    Peter - alotofstuffhere[dot]com - Email Me - Donate via PayPal - Got spare hardware? Donate 'em to me :) Just send me a PM.
    Currently: enjoying the early holidays :)
    Read before posting: FAQ | What you CAN'T do with JavaScript | Form Rules | Thread Title Naming Guide

  3. #3
    Join Date
    Aug 2006
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by tech_support View Post
    Code:
    <div onclick="javascript:toggleBgColor(document.getElementById('testing-element'));">testing</div>
    <div id="testing-element">change this color</div>
    wow, thanks. that was quick.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •