| | |
change background color and font color
Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Feb 2009
Posts: 9
Reputation:
Solved Threads: 0
Hey Guys,
I am frustrated with this matter and therefore need some help. I will try to keep this as simple as possible. I really need some help performing something that should be relatively simple in Javascript. I have a form which contains FOUR text fields (e.g. TEXTFIELD1, TEXTFIELD2, TEXTFIELD3 & TEXTFIELD4). Each text field holds a HEX,DEC color value. ABOVE this form I have a table with TWO ROWS (ROW1 and ROW2). ROW1 should correspond with TEXTFIELD1, so that when the VALUE in TEXTFEILD1 is changed the background color of ROW1 will change to match the HEX,DEC VALUE entered in TEXTFIELD1. The same would happen with TEXTFIELD2 and ROW2. TEXTFIELD3 should be used to change the color of the TEXT inside ROW1 and TEXTFIELD4 should change the color of the TEXT in ROW2. I also wanted to know if it would be possible to achieve this without clicking any button. If doing this without clicking a button is overly complicated, i havinga button is not a problem.
------------------------------------------------------------------
| ROW1 | TEXT IN ROW 1
------------------------------------------------------------------
| ROW2 | TEXT IN ROW 2
------------------------------------------------------------------
TEXTFIELD1 <----HEXDEC VALUE GOES HERE to change color of ROW1---->
TEXTFIELD2 <----HEXDEC VALUE GOES HERE to change color of ROW2---->
TEXTFIELD3 <----HEXDEC VALUE GOES HERE to change color of TEXT IN ROW1---->
TEXTFIELD4 <----HEXDEC VALUE GOES HERE to change color of TEXT in ROW2---->
Hope this makes sense.
Thanks,
E.A.
I am frustrated with this matter and therefore need some help. I will try to keep this as simple as possible. I really need some help performing something that should be relatively simple in Javascript. I have a form which contains FOUR text fields (e.g. TEXTFIELD1, TEXTFIELD2, TEXTFIELD3 & TEXTFIELD4). Each text field holds a HEX,DEC color value. ABOVE this form I have a table with TWO ROWS (ROW1 and ROW2). ROW1 should correspond with TEXTFIELD1, so that when the VALUE in TEXTFEILD1 is changed the background color of ROW1 will change to match the HEX,DEC VALUE entered in TEXTFIELD1. The same would happen with TEXTFIELD2 and ROW2. TEXTFIELD3 should be used to change the color of the TEXT inside ROW1 and TEXTFIELD4 should change the color of the TEXT in ROW2. I also wanted to know if it would be possible to achieve this without clicking any button. If doing this without clicking a button is overly complicated, i havinga button is not a problem.
------------------------------------------------------------------
| ROW1 | TEXT IN ROW 1
------------------------------------------------------------------
| ROW2 | TEXT IN ROW 2
------------------------------------------------------------------
TEXTFIELD1 <----HEXDEC VALUE GOES HERE to change color of ROW1---->
TEXTFIELD2 <----HEXDEC VALUE GOES HERE to change color of ROW2---->
TEXTFIELD3 <----HEXDEC VALUE GOES HERE to change color of TEXT IN ROW1---->
TEXTFIELD4 <----HEXDEC VALUE GOES HERE to change color of TEXT in ROW2---->
Hope this makes sense.
Thanks,
E.A.
Here's a simple demo, that you can work with.
Hope it helps...
Hope it helps...
javascript Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head profile="http://www.w3.org/2005/10/profile"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>Manipulating Background color using textfield's</title> <style type="text/css"> /* <![CDATA[ */ body, div, html { border: none; font: normal normal normal 100%/115% "Trebuchet MS", Verdana, Arial, sans-serif; } caption { caption-side: top; display: table-caption; margin-bottom: .5em; text-align: left; } body { background-color: #eee; color: #006; text-align: center; width: 100%; } body div#tube { background-color: inherit; color: inherit; left: 1.2em; position: relative; top: 2em; width: 96%; } body div#wrapper { background-color: #fff; color: #696969; margin: 0 auto; min-height: 16.5em; overflow: hidden; position: relative; width: 98%; } div { margin: 0; padding: 0; } html { min-width: 60em; width: auto; } label { display: block; } input { display: inline-block; vertical-align: middle; } table { border: none; border-collapse: collapse; color: #696969; margin: 0; padding: 0; text-align: left; white-space: nowrap; min-width: 30em; } tr { line-height: 130%; } td { border: thin solid; padding: .5em 1em .5em 1em; width: auto; vertical-align: middle; } th { border-top: medium solid; padding-left: 1em; } /* ]]> */ </style> <script type="text/javascript"> /* <![CDATA[ */ var hexdec = /^[\#]{1}[\d-a-fA-F]{6}$/; function demo(thisValue,thisRef,txtBg) { var rows = document.getElementsByTagName('td'); var fields = document.forms['frm']; if(!hexdec.test(thisValue.value)){do { var val = prompt('Please enter a valid Hexdec Value\n(e.g. #000AAA)'); } while(!hexdec.test(val)); eval('fields.' + thisValue.id + '.value=val'); } if ( txtBg ) { rows[thisRef].style.backgroundColor = thisValue.value; } else { rows[thisRef].style.color = thisValue.value; } } /* ]] */ </script> </head> <body> <div id="wrapper"> <div id="tube"> <form action="#" id="frm" onsubmit="return false;"> <table summary="JavaScript Demo"> <caption>JavaScript Demo</caption> <tr> <th><label>Text Colour</label></th> <th><label>Background Colour</label></th> </tr> <tr> <td id="row1" colspan="2">Designated to TextField 1(colour) and 2(bgcolour)</td> </tr> <tr> <td id="row2" colspan="2">Designated to TextField 3(colour) and 4(bgcolour)</td> </tr> <tr> <td><label>Colour: <input type="text" id="colour1" name="colour1" value="textfield 1" onchange="demo(this,'row1');" /></label></td> <td><label>BgColour: <input type="text" id="bgcolour1" name="bgcolour1" value="textfield 2" onchange="demo(this,'row1',true);" /></label></td> </tr> <tr> <td><label>Colour: <input type="text" id="colour2" name="colour2" value="textfield 3" onchange="demo(this,'row2');" /></label></td> <td><label>BgColour: <input type="text" id="bgcolour2" name="bgcolour2" value="textfield 4" onchange="demo(this,'row2',true);" /></label></td> </tr> </table> </form> </div> </div> </body> </html>
Last edited by essential; Feb 12th, 2009 at 1:22 am.
•
•
Join Date: Feb 2009
Posts: 9
Reputation:
Solved Threads: 0
Hello Essential,
First and foremost I REALLY appreciate the demo code. The only issue that I'm having is that when I try the demo the none of the colours are changing (neither text nor background). I enter valid HEX,DEC values but the colours fail to change. Is there any reason why this is happening?
Your help is greatly appreciated.
Thanks
E.A.
"From Java to LAMP, a few miles to go and I'll be totally AMPED"
First and foremost I REALLY appreciate the demo code. The only issue that I'm having is that when I try the demo the none of the colours are changing (neither text nor background). I enter valid HEX,DEC values but the colours fail to change. Is there any reason why this is happening?
Your help is greatly appreciated.
Thanks
E.A.
"From Java to LAMP, a few miles to go and I'll be totally AMPED"
This look a bit more complicated than other one. But this will fix the issue. Enjoy...
javascript Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="utf-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head profile="http://www.w3.org/2005/10/profile"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta http-equiv="Content-Script-Type" content="text/javascript" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title>Manipulating Background color using textfield's</title> <style type="text/css"> /* <![CDATA[ */ body, div, html { border: none; font: normal normal normal 100%/115% "Trebuchet MS", Verdana, Arial, sans-serif; } caption { caption-side: top; display: table-caption; margin-bottom: .5em; text-align: left; } body { background-color: #eee; color: #006; text-align: center; width: 100%; } body div#tube { background-color: inherit; color: inherit; left: 1.2em; position: relative; top: 2em; width: 96%; } body div#wrapper { background-color: #fff; color: #696969; margin: 0 auto; min-height: 16.5em; overflow: hidden; position: relative; width: 98%; } div { margin: 0; padding: 0; } html { min-width: 60em; width: auto; } label { display: block; } input { display: inline-block; vertical-align: middle; } table { border: none; border-collapse: collapse; color: #696969; margin: 0; padding: 0; text-align: left; white-space: nowrap; min-width: 30em; } tr { line-height: 130%; } td { border: thin solid; padding: .5em 1em .5em 1em; width: auto; vertical-align: middle; } th { border-top: medium solid; padding-left: 1em; } /* ]]> */ </style> <script type="text/javascript"> /* <![CDATA[ */ var hexdec = /^[\#]{1}[\d-a-fA-F]{6}$/; var ua = { ref : function( id ) { if ( document.all ) { return document.all[id]; } else if ( document.getElementById ) { return document.getElementById(id); } else { return document.layers[id]; } } } function demo(e) { e = e ? e : window.event; t = e.target ? e.target : e.srcElement; if (( ua.ref ) && ( t.tagName.toLowerCase() == 'input' )) { if ( !hexdec.test(t.value) ) { do { var msg = prompt('Please enter a valid hexdec value!\n(e.g. #000AAA)'); } while(!hexdec.test(msg)); t.value = msg; } for ( var x = 0; x <= 1; x++ ) { if ( t.name == (('colour') + x ) ) { ua.ref((('row') + x)).style.color = t.value; } if ( t.name == (('bgcolour') + x )) { ua.ref((('row') + x)).style.backgroundColor = t.value; } } } } if (document.addEventListener) document.addEventListener('change',demo,false); else if (document.attachEvent) document.attachEvent('onchange',demo); else document.onchange = demo; /* ]]> */ </script> </head> <body> <div id="wrapper"> <div id="tube"> <form action="#" id="frm" onsubmit="return false;"> <table summary="JavaScript Demo"> <caption>JavaScript Demo</caption> <tr> <th><label>Text Colour</label></th> <th><label>Background Colour</label></th> </tr> <tr> <td id="row0" colspan="2">Designated to TextField 1(colour) and 2(bgcolour)</td> </tr> <tr> <td id="row1" colspan="2">Designated to TextField 3(colour) and 4(bgcolour)</td> </tr> <tr> <td><label>Colour: <input type="text" id="colour0" name="colour0" value="textfield 1" /></label></td> <td><label>BgColour: <input type="text" id="bgcolour0" name="bgcolour0" value="textfield 2" /></label></td> </tr> <tr> <td><label>Colour: <input type="text" id="colour1" name="colour1" value="textfield 3" /></label></td> <td><label>BgColour: <input type="text" id="bgcolour1" name="bgcolour1" value="textfield 4" /></label></td> </tr> </table> </form> </div> </div> </body> </html>
•
•
Join Date: Feb 2009
Posts: 9
Reputation:
Solved Threads: 0
Essential,
Wow, you are right! To be honest, i was testing this code using the lastest Firefox browser and Google's chrome. Maybe this is much to ask but how would I go about making this code compatible with other browsers, particularly Firefox... and Chrome if possible?
I really appreciate your help/feedback. I am a Java guy but I am getting into LAMP now. Thanks for everything.
E.A.
Wow, you are right! To be honest, i was testing this code using the lastest Firefox browser and Google's chrome. Maybe this is much to ask but how would I go about making this code compatible with other browsers, particularly Firefox... and Chrome if possible?
I really appreciate your help/feedback. I am a Java guy but I am getting into LAMP now. Thanks for everything.
E.A.
The best way to do this is to use the navigator object to detect different types of browser including versions.
In line 80 simply replace the statement block with this:
hope this will finalized the issue. Good day to you...
In line 80 simply replace the statement block with this:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
if (ua.ref && t.id) {
•
•
Join Date: Feb 2009
Posts: 9
Reputation:
Solved Threads: 0
Hey Essential,
Once again, you are super. Thanks a bunch. Just a couple of points I wanted to mention. The first set of code that you posted, as per your message, works perfectly in IE and Opera. The second code that you posted, however, doesn't seem to work at all. I'm wondering if it is something that I'm doing wrong. When I tested the second (most recently posted) modification, none of the colors changed - not even in IE 6. On that note, I really appreciate the help you provided about how to make the code work for other browsers. If I can get the second code to work I think I'll be able to work with what you have showed me. I really appreciate your help in this matter, as it is very important that I get past this hurdle. Thanks a million
E.A.
Once again, you are super. Thanks a bunch. Just a couple of points I wanted to mention. The first set of code that you posted, as per your message, works perfectly in IE and Opera. The second code that you posted, however, doesn't seem to work at all. I'm wondering if it is something that I'm doing wrong. When I tested the second (most recently posted) modification, none of the colors changed - not even in IE 6. On that note, I really appreciate the help you provided about how to make the code work for other browsers. If I can get the second code to work I think I'll be able to work with what you have showed me. I really appreciate your help in this matter, as it is very important that I get past this hurdle. Thanks a million
E.A.
![]() |
Similar Threads
- JTextArea & Font Color (Java)
- change font type, font color, font size and bacground color in listbox (JavaScript / DHTML / AJAX)
- Change the background of "active" button (JavaScript / DHTML / AJAX)
- Color change problem (Java)
- How to change backround colour in WordXP (Windows Software)
- The size of images change on different pcs (Site Layout and Usability)
- How do i change scrollers and make my site sutible for all resolutions? (HTML and CSS)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: editing div tag
- Next Thread: ajax login
| Thread Tools | Search this Thread |
acid2 ajax ajaxcode ajaxhelp animate array automatically beta box bug calendar cart checkbox class codes column cookies createrange() css cursor date debugger decimal design dom download dropdown editor element enter error events explorer firefox focus form frameworks getselection google gwt html htmlform iframe image() images index internet java javascript jawascriptruntimeerror jquery jsf jsfile jsp jump listbox maps masterpage math menu microsoft mimic mp4 object onmouseover parent php player post problem programming progressbar prototype rating redirect regex runtime safari scale scriptlets search select session shopping size sql starrating text textarea toggle validation variables w3c web website window windowofwords windowsxp wysiwyg \n





