JavaScript - onclick attribute to be set dynamically to anchor tag in a table

Asked By Anandh Ramanujam on 29-Dec-10 09:56 AM
Hi,

Im creating new row to the table dynamically.

While creating a cell to that row, I dynamically create an anchor element and I set onclick attribute to another function with parameter 'row' object.

I did like this,

function AddRow()
{
.........

var rowCount=tblAttach.rows.length;

var row = tblAttach.insertRow(rowCount);

var cell1 = row.insertCell(0);

var element1 = document.createElement("a");

element1.setAttribute('onclick','javascript:RemoveRow(' + row + ');'); // set onclick attribute with parameter 'row' object
var cell3=row.insertCell(1);
.........
.........

}
function RemoveRow(obj)
{
  alert(obj); //  Here an error message occur 'runtime error: 'object' is undefined'
}


Can anybody help me to sorted out this problem...

And one more I want to know that, whether the dynamically created html contents can be viewed in 'View Source' code...

Thanks,
Anandh
Rolf Jaeger replied to Anandh Ramanujam on 29-Dec-10 06:31 PM
Hi Anandh:

I can't test my suspicion, but if I was you I would declare the variable 'row' at a higher scope, i.e. not within the 'AddRow' function. That MIGHT do the trick.

Hope this helped.
Rolf