Click to See Complete Forum and Search --> : Setting onclick in Javascript.


zengonzo
12-19-2002, 12:21 PM
I'm creating rows on the fly. When someone presses the add button, a new row pops up. The problem is that I need that row to have functionality, too.

td = row.insertCell();
var qButton = td.appendChild(document.createElement('image'));
qButton.onclick = '"alert(1);"'

Just for an example. This won't fire off an alert when you click the newly created button. When I check out the innerHTML, it reads okay, but the onclick is still useless.

I've tried a number of things .. Taking out the quotation marks, leaving them in .. I've made this work before, but I was using innerHTML, which I can't do right now, as I need it supported for Netscape. Any clues? I'd appreciate it.

swon
12-19-2002, 12:31 PM
Did you tried it with setAttribute();?

for example:


td = row.insertCell();
var qButton = td.appendChild(document.createElement('image'));
qButton.setAttribute("onClick","alert('1')");

zengonzo
12-19-2002, 12:38 PM
Originally posted by swon
Did you tried it with setAttribute();?

Yes, tried that one. The only thing that has worked for me so far is setting innerHTML ..

td.innerHTML = '<IMG SRC="static/delete.gif" ONCLICK="alert(1);">'

Is there something like that supported by Netscape?

swon
12-19-2002, 12:49 PM
I think Netscape >6 does it

zengonzo
12-19-2002, 12:52 PM
Well, until I find another option that's the way I have to go .. maybe I can just use clone element .. Thanks for taking the time. :)

Rick Bull
12-19-2002, 01:21 PM
Try this:

qButton.onclick = new Function('alert(1);');

zengonzo
12-19-2002, 01:30 PM
[QUOTE]Originally posted by Rick Bull
qButton.onclick = new Function('alert(1);'); [/B]/QUOTE]
That did it! Much obliged.