/    Sign up×
Community /Pin to ProfileBookmark

Setting onclick in Javascript.

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.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@swonDec 19.2002 — Did you tried it with setAttribute();?

for example:


td = row.insertCell();

var qButton = td.appendChild(document.createElement('image'));

qButton.setAttribute("onClick","alert('1')");
Copy linkTweet thisAlerts:
@zengonzoauthorDec 19.2002 — [i]Originally posted by swon [/i]

[B]Did you tried it with setAttribute();?[/B][/QUOTE]


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?
Copy linkTweet thisAlerts:
@swonDec 19.2002 — I think Netscape >6 does it
Copy linkTweet thisAlerts:
@zengonzoauthorDec 19.2002 — 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. ?
Copy linkTweet thisAlerts:
@Rick_BullDec 19.2002 — Try this:

qButton.onclick = new Function('alert(1);');
Copy linkTweet thisAlerts:
@zengonzoauthorDec 19.2002 — [i]Originally posted by Rick Bull [/i]
qButton.onclick = new Function('alert(1);'); [/B]/QUOTE]

That did it! Much obliged.
×

Success!

Help @zengonzo spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.21,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...