Click to See Complete Forum and Search --> : How To set onclick in dynamicallly created <tr>


onerrorgoto
09-05-2003, 04:22 AM
Hello
I have a function that adds a row to my Table.
I just cant get this function to set the onclick of the <tr>-tag.

what am i doing wrong?


function lvwRefToStd_addRow2(text1,text2)
{
var rowid;
var rownr;
var tbl;
var newrowid;
var oTr;
var oTd;

//get the last rowid, -1 to avoid the <Tfooter>
rowid=document.all.tbllvwRefToStd.rows[document.all.tbllvwRefToStd.rows.length-1].id
//extract rownr from rowid
rownr=parseFloat(rowid.substring(rowid.indexOf("_")+2,rowid.length));
//extract table-name from rowid
tbl=rowid.substring(0,rowid.indexOf("_"));
//create new rowid
newrowid=tbl + "_r" + (rownr+1);
//add new row
oTr=document.all.tbllvwRefToStd.insertRow(document.all.tbllvwRefToStd.rows.length-1);
oTr.id=newrowid;
oTr.onClick="javascript:changecolor(this);javascript:lvwRefToStd_saveRowID(this)";

//create one column / inparam
oTd=oTr.insertCell()
oTd.innerHTML += text1;
oTd=oTr.insertCell()
oTd.innerHTML += text2;
}


Thanks
Anders

Fang
09-05-2003, 05:16 AM
Use setAttribute. javascript is one word (split for security).
Try it out in IE and Mozilla, if it works in MOz. and not in IE then IE doesn't support DOM handler setting.

oTr.setAttribute("onclick", "javascript:changecolor(this);javascript:lvwRefToStd_saveRowID(this)");

DOSPrompt
09-05-2003, 05:43 AM
Couple of things... document.all is an IE thing, so it won't work in Netscape/Mozilla. And when setting a handler dynamically, you have to set it to a function, not a string!

When you write inline functions in HTML, the browser is intelligent enough to know that you're writing a function. But when you do it dynamically like this, it assumes you're just writing a string, which is exactly what you _are_ doing.

Try something like...
oTr.onclick = function () {
changecolor(this);
lvwRefToStd_saveRowID(this)";
}

I'm not 100% certain about this, but I think you need to use lower case rather than camel case when assigning handlers, ie. onclick rather than onClick.

Hope this helps.

onerrorgoto
09-05-2003, 06:06 AM
Thanks for the replies.

Im only working against IE (customer demand)

I will try both answers and get back to you with the one thats working (or both ;) )

/Anders

onerrorgoto
09-05-2003, 06:14 AM
Hello again
Try it out in IE and Mozilla, if it works in MOz. and not in IE then IE doesn't support DOM handler setting.
Fang: Not working, bad IE :mad:

you need to use lower case rather than camel case when assigning handlers, ie. onclick rather than onClick
:)
dosprompt: lowercase it was and your code worked just fine.

Now I jsut have to get the functions that is called to work ;)

Thanks alot for the help
/Anders

Fang
09-07-2003, 07:24 AM
I ran into the same problem - thanks DOSPrompt
Another problem with IE was creating radio buttons :mad:
The solution was on the archives here (http://www.xs4all.nl/~ppk/js/list.html), for future reference.

Khalid Ali
09-07-2003, 10:00 AM
Proper way to add an event like that is using DOM method

objRef.addEventListener("onclick",false,functionName);

the above works in NS6+(though it should in IE too,but IE is too freaking lazy to implement DOM standards)

and for IE

objRef.attachEvent("onclick,functionName);

where function name above is the function that you want to be triggered when a user clicks on that obj.