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 :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.