Web Hosting Talk







View Full Version : javascript .. using function() {} in onclick


matt2kjones
11-02-2009, 10:08 AM
Hey,

I'm have a problem with javascript in internet explorer.

Basically, in any other browser, i can use setatribute to assign the onlick event to occur when the user clicks a row in a table that i have dynamically generated with javascript. However, in internet explorer that doesn't work.

However, using function() {} seems to work, but is causing me a new problem. See the code below:


ResultsTable.rows[RowID].onclick = function() { ShowDocument(RowID); };

Basically, what is happening is, each time i create a new row in the table, i run that above code to assign the onclick event.

But because function() {} creates a pointer to the function, the function doesn't get evaluated each time a row is added.

Basically, RowID, as the parameter for ShowDocument.... is always the ID of the Last Row... not the ID when it was creating the row.

How can i get around this? Any Idea's?

Cheers

sasha
11-02-2009, 12:09 PM
Maybe this will help.
http://tnt.goldnet.ca/wht/anonfunc.html

You can use keywerd "this" or properties of "event" to find out row id.

stahightech
11-02-2009, 12:40 PM
Hi,

I would probably pass the 'this' keyword and use it to find the RowID. Actually, you could just use the rowIndex property of the DOM object, if that would suffice.

mattle
11-02-2009, 01:24 PM
jQuery's standardization of event binding may be of use here.

stahightech
11-02-2009, 01:25 PM
After coding a quick example. I would use the event to find the row id as @sasha suggested. Something along these lines:

ResultsTable.rows[RowID].onclick = function() { ShowDocument(event); };

function ShowDocument(event)
{
// IE uses srcElement.
var target = event.target ? event.target : event.srcElement;

// target show be the cell within the table, so the parentNode should be the row
alert(target.parentNode.rowIndex);
}

matt2kjones
11-04-2009, 05:03 AM
Thanks for all the help.

I have solved this problem

Thank you