Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: When checkbox is selected backround is different

  1. #1
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default When checkbox is selected backround is different

    Can anyone tell me a script where when you click on a check box and it is selected it will change the background color of the <td> or <tr> box like in gmail.com and yahoo mail (main original version)?

  2. #2
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Try this:

    Code:
    <form>
    
    <table>
     <tr id="example" style="background-color: #CCCCCC;">
       <td><input type="checkbox" name="check" value="yes" onclick="if (document.getElementById('example').style.background-color == '#CCCCCC') { document.getElementById('example').style.background-color = '#EEEEEE';} else {document.getElementById('example').style.background-color = '#CCCCCC';}"> Some text here test test test blahblahblah</td>
     </tr>
    </table>
    
    </form>
    Probably not the cleanest way to do it, but it should work. Hope this helps.
    Last edited by thetestingsite; 04-21-2007 at 04:53 PM.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  3. #3
    Join Date
    Feb 2007
    Posts
    293
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    You can also do it with innerHTML

    Code:
    <script type="text/javascript">
    function changeColor(){
    	var origHTML = document.getElementById('colorMe').innerHTML;
    	var newHTML = "<span style='background-color:red'>" + origHTML + "</span>";
    	document.getElementById('colorMe').innerHTML = newHTML;
    }
    </script>
    <input type='radio' onclick='changeColor()' value='Change Text'/>
    <table>
    <tr><td id="colorMe" style='background-color:#ffffff'>this is the table cell</td></tr>
    </table>

  4. #4
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Quote Originally Posted by thetestingsite
    <form>

    <table>
    <tr id="example" style="background-color: #CCCCCC;">
    <td><input type="checkbox" name="check" value="yes" onclick="if (document.getElementById('example').style.background-color == #CCCCCC) { document.getElementById('example').style.background-color = #EEEEEE;} else {document.getElementById('example').style.background-color = #CCCCCC;}"> Some text here test test test blahblahblah</td>
    </tr>
    </table>

    </form>
    Hex values have to be strings. So:
    Code:
    if (condition) document.getElementBy(..).style.background = '#FF0000';
    - Mike

  5. #5
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Ah yes, forgot about those. Thanks, and it is fixed now in my above post.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  6. #6
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    The conditional operator exists to make things like this more readable:
    Code:
    <input type="checkbox" name="check" value="yes" onclick="
      var es = document.getElementById('example').style;
      es.backgroundColor = (es.backgroundColor === '#cccccc' ? '#eeeeee' : '#cccccc');
    ">
    Also, in scripting the property is called backgroundColor, not background-color.
    Quote Originally Posted by Veronica
    You can also do it with innerHTML
    Although why on Earth you'd want to when there are non-innerHTML methods available is beyond me. See the SlayerOffice article Alternatives to innerHTML.
    Also, <span> is an inline element, so the effect may be other than what the OP intended, if the element to be coloured is a block-level element.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  7. #7
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    It would even be pointless to rewrite existing nodes, when the attribute style.backgroundColor will work fine.
    Also:
    Quote Originally Posted by Twey
    es.backgroundColor = (es.backgroundColor === '#cccccc' ? '#eeeeee' : '#cccccc');
    Why would you need to return a boolean value?
    Code:
    es.backgroundColor = (es.backgroundColor == '#cccccc' ? '#eeeeee' : '#cccccc');
    Wouldn't that work fine?
    - Mike

  8. #8
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Any comparison returns a boolean value. === prevents type-casting when checking: (0 == "") returns true, but (0 === "") returns false. Using === wherever possible is good style since it reduces ambiguity and I suspect it to be slightly more efficient in most implementations, although I haven't run benchmarks.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  9. #9
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    Stupid me... So it reads strings differently then numbers.
    - Mike

  10. #10
    Join Date
    Feb 2007
    Posts
    293
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    So just to help motormichael12... your final answer would be:

    Code:
    <table>
     <tr id="example" style="background-color: #CCCCCC;">
       <td><input type="checkbox" name="check" value="yes" onclick="
      var es = document.getElementById('example').style;
      es.backgroundColor = (es.backgroundColor === '#cccccc' ? '#eeeeee' : '#cccccc');
    "> Some text here test test test blahblahblah</td>
     </tr>
    </table>
    yes?

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
  •