ASP.NET - onclick attribute to be set dynamically to anchor tag in a table

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

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


anil soni replied to Anandh Ramanujam on 29-Dec-10 10:19 AM
You are passing the incorrect parameter you should pass rowCount instead i.e the code should be like

element1.setAttribute('onclick','javascript:RemoveRow(' + row.rowIndex + ');');

and in RemoveRow function you will get the index of row clicked

function RemoveRow(rowIndex)
  alert(rowIndex); //  Here an error message occur 'runtime error: 'object' is undefined'
  //if the row object is requried
  var row = tblAttach.rows[rowIndex];

  //if you want to remove the row from the table
Anandh Ramanujam replied to anil soni on 29-Dec-10 11:18 AM
Thanks for your reply...

I found the solution, here I like to share with you all....

// instead of write, element1.setAttribute('onclick','javascript:RemoveRow(' + row + ');');
// I wrote the event for onclick as below. it works great....


function RemoveRow(rowobj)


    alert('row deleted');