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)?
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)?
Try this:
Probably not the cleanest way to do it, but it should work. Hope this helps.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>
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
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>
Hex values have to be strings. So:Originally Posted by thetestingsite
Code:if (condition) document.getElementBy(..).style.background = '#FF0000';
- Mike
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
The conditional operator exists to make things like this more readable:Also, in scripting the property is called backgroundColor, not background-color.Code:<input type="checkbox" name="check" value="yes" onclick=" var es = document.getElementById('example').style; es.backgroundColor = (es.backgroundColor === '#cccccc' ? '#eeeeee' : '#cccccc'); ">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.Originally Posted by Veronica
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!
It would even be pointless to rewrite existing nodes, when the attribute style.backgroundColor will work fine.
Also:
Why would you need to return a boolean value?Originally Posted by Twey
Wouldn't that work fine?Code:es.backgroundColor = (es.backgroundColor == '#cccccc' ? '#eeeeee' : '#cccccc');
- Mike
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!
Stupid me... So it reads strings differently then numbers.
- Mike
So just to help motormichael12... your final answer would be:
yes?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>
Bookmarks