Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onLoad function (error sending object reference)

Status
Not open for further replies.

lazyRascal

Programmer
Feb 27, 2006
22
US
Can anyone help me with some onLoad syntax?
The following code highlights the contents of a row in a table in my html page when clicked. It works when clicked.

I would like to have the first row selected automatically when the page is loaded. I've tried every way I can think of to execute it from within the <body> tag using the onLoad event. I can't get it to work. Any help would be greatly appreciated.

This snippet is just a striped down version of the actual code to demonstrate what I'm trying to accomplish; therefore, I'm not looking for alternative methods to get these results. I need to make this approach work, if possible.

I've used <body onLoad="javascript:'selected(0)'">. I think the problem is that I'm sending a value rather than an object.

I have the following code:
Code:
<html>
<head>
<script language="JavaScript">

var curSelected = null;
function selected(row)
{
	row.style.backgroundColor = 'cc7777';
	row.style.color = 'white';

	if (curSelected != null)
	{
		curSelected.style.backgroundColor = '';
		curSelected.style.color = '';
	}
	curSelected = row;
}
</script>
</head>

<body>
<table border="0" cellpadding="3" cellspacing="0">
<tr onClick="selected(this)">
  <td width=45><img src="hammer.jpg" height="60" border="2" style="border-color: #ffffff"></td>
  <td>Hammer</td>
</tr>
<tr onClick="selected(this)">
  <td width=45><img src="screwdriver.jpg" height="60" border="2" style="border-color: #ffffff"></td>
  <td width='100'>Screwdriver</td>
</tr>
<tr onClick="selected(this)">
  <td width=45><img src="pliers.jpg" height="60" border="2" style="border-color: #ffffff"></td>
  <td>Pliers</td>
</tr>
</table>

</body>
</html>
 
I think the problem is that I'm sending a value rather than an object.

you are right. try something like this...

1) give your table an id
Code:
<table id="myTable">

2) call the function from your onload event like this:
Code:
<body onload="selected( document.getElementById('myTable').rows[0] );">

this will pass the first row of the table to the function, rather than the number 0.



hope this helps.



*cLFlaVA
----------------------------
spinning-dollar-sign.gif
headbang.gif
spinning-dollar-sign.gif

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Awesome! I've been working on this since early yesterday. Thank you so much. You just can't imagine how helpful this forum is to newbies. Have a wonderful day.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top