Jump to content

[SOLVED] Simple edit to existing working javascript function


Jason28

Recommended Posts

Hello, I am using a function I found that changes the background color of a <tr> column when a checkbox is unchecked.  I would like to make one small change... currently you can only use colors such as "red, blue, green, etc." to submit to the function.  I would like to be able to use hexi colors like #FF0000 and such.  Here is the code:

 

<script type="text/javascript">
		//var list = document.getElementsByTagName("tbody")[0].getElementsByTagName("tr");

		function toggleColour(obj,col)
		{
		col=col||'transparent'
		var row = findParentRow(obj);
		if (!row.col) row.col=row.style.backgroundColor;
		row.style.backgroundColor = obj.checked ? row.col : col; 
		}

		function findParentRow(obj)
		{
		var tmp = obj.parentNode;

		if(tmp.nodeName.toLowerCase() != "tr")
		{
		tmp = findParentRow(tmp);
		}

		return tmp;
		}
</script>


<table>
  <tr style="background-color:white;">
    <td class="bigtext"><input type="checkbox" value="1" name="chk[1]" checked="checked" onClick="toggleColour(this,'red');" /></td>
    <td class="bigtext">test 1</td>
  </tr>
  <tr style="background-color:white;">
    <td class="bigtext"><input type="checkbox" value="1" name="chk[2]" checked="checked" onClick="toggleColour(this,'red');" /></td>
    <td class="bigtext">test 2</td>
  </tr>
</table>

 

You can see in the html where onClick="toggleColour(this,'red') passes the color red, I would like to use html hexi colors in that field instead of just 'red'. It doesn't seem to work if you just add a hexi color there. Thanks :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.