I have a piece of code shown below that is supposed to select the first object in a list of image object. When it runs, about half of the time I am getting an error:
TypeError: Cannot read property of mylist
I realize that this is happening because it is trying to reference an object that has not yet been given a reference to an object. Basically, the page will load, and I want to call this function in JavaScript, however if this code runs before the list is referenced, it errors out. Here is my code snippet:
window.onload = function () {
try {
var tnl = $find('mylist');
tnl.selectFirstItem();
}
catch (exception) {
alert(exception);
}
}
The
tnl.selectFirstItem()
line is the one throwing the error to the catch block.
So, my question is, is there a way to force it to wait until this object "tnl" has been referenced?
Thanks!