View Full Version : Dynamic onclick add to TD not working :(

07-02-2011, 01:27 PM
Hi folks,

I'm frustrated :(. I've been trying to get this single piece of code to work. I've looked at quite a few sources out there, but everything I try fails.

My site uses AJAX to upload a file, and once complete I dynamically add the file name, size, description, etc to a table.

I have a javascript method that will allow me to download the file once uploaded - and I put an onclick event into some of the TDs in the row I added. (Note: I didn't want to put it in the TR because I also have a delete button in the row).

I've tried

1. cell1.onclick = 'javascript:downloadAttachmentFile('+attachID+');';
2. cell1["onclick"] = new Function("downloadAttachmentFile('+attachID+')");
3. cell3.setAttribute('onclick','javascript:downloadAttachmentFile('+attachID+');');

but none seem to work!! I know the function is ok because the files that are already on the page work correctly, meaning it's only the dynamically created rows that are having the issues.

function addNewTableRow(fileTable, fileTitle, fileName)
var table = getMyElement(fileTable);
var rowCount = table.rows.length;
var row = table.insertRow(rowCount);
var cell0 = row.insertCell(0);
cell0.width = 25;
cell0.className = "tblTicketFilesWhite";
cell0["onclick"] = new Function("downloadAttachmentFile('+attachID+')");

Logic Ali
07-02-2011, 02:57 PM
This isn't the best way to do it, but it's closest to what you had:

cell0.onclick = new Function( "downloadAttachmentFile('" + attachID + "')" );