Forum Moderators: open

Message Too Old, No Replies

Need Help with changing table row colors

         

eawade

4:38 pm on Feb 10, 2009 (gmt 0)

10+ Year Member



Hey Guys,

I will try to keep this as simple as possible. I really need some help performing a relatively simple Javascript function. I have a form which contains two text fields (e.g. TEXTFIELD1 and TEXTFIELD2). Each text field holds a HEX,DEC color value. ABOVE this form I have a table with TWO ROWS. ROW1 should correspond with TEXTFIELD1, so that when the VALUE in TEXTFEILD1 is change the background color of ROW1 will change to match the HEX,DEC VALUE entered in TEXTFIELD1. The same would happen with TEXTFIELD2 and ROW2. I also wanted to know if it would be possible to achieve this without clicking any button. Basically once the value is input in the field the background of the corresponding row changes.

------------------------------------------------------------------
¦ ROW1 ¦
------------------------------------------------------------------
¦ ROW2 ¦
------------------------------------------------------------------

TEXTFIELD1 <----HEXDEC VALUE GOES HERE to change color of ROW1---->
TEXTFIELD2 <----HEXDEC VALUE GOES HERE to change color of ROW2---->

I apologize if I have written too much guys. Any help would be greatly appreciated.

Thanks,
E.A.

Fotiman

6:29 pm on Feb 10, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld! Yes, you can do this without having to press a button. The events for the text fields that might be of interest to you would be the onchange event, the onkeyup event, or the onblur event. Here's a quick example. Note, this example attaches event handlers (which can be overwritten or might be overwriting another handler) a better approach would be to attach event listeners, but for the sake of simplicity I'm only showing the event handlers. Also, this example does not do any validation. When the value changes (which occurs when focus moves away from the input and the value has changed) it will set the background color, so you should enter a value like "#c00" or "red". You would probably want some sort of validation before setting the background color.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled</title>
</head>
<body>
<div>
<table id='myTable'>
<tbody>
<tr><td>Row 1</td></tr>
<tr><td>Row 2</td></tr>
</tbody>
</table>
<form action="">
<div>
<div><input type="text" id="row1color"></div>
<div><input type="text" id="row2color"></div>
</div>
</form>
</div>
<script type="text/javascript">
window.onload = function () {
// Attach event handler
var r1input = document.getElementById('row1color'),
r2input = document.getElementById('row2color'),
tbody = document.getElementById('myTable').getElementsByTagName('tbody')[0];
r1input.onchange = function () {
tbody.getElementsByTagName('tr')[0].style.backgroundColor = r1input.value;
}
r2input.onchange = function() {
tbody.getElementsByTagName('tr')[1].style.backgroundColor = r2input.value;
}
};
</script>
</body>
</html>

eawade

7:17 pm on Feb 16, 2009 (gmt 0)

10+ Year Member



Hey Fotiman,

I wanted to say thankyou for the code. It was a big help.

Thanks
Ethan-Anthony