/    Sign up×
Community /Pin to ProfileBookmark

Hi !

I’d like to provoke a reaction on a click. I’d like to do it in Dhtml because i don’t want to rewrite the object. I wrote :

[code=php]document.getElementById(‘ff’).onclick=alert(”)[/code]

but the alert appears at the loading, which is not the wanted effect. Could you let me know the good syntax, please ?

Thanx for the help !

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@MongusMar 19.2005 — The way you're doing it you're assigning the result of the alert call (undefined) to onclick.

Try this:

[code=php]
document.getElementById('ff').onclick=function(e){alert('');};
[/code]
Copy linkTweet thisAlerts:
@A1ien51Mar 19.2005 — How many places did you ask this question....
Copy linkTweet thisAlerts:
@Grand_MamamouchauthorMar 19.2005 — Thank you, Mongus, for your answer !

It works now !
Copy linkTweet thisAlerts:
@Grand_MamamouchauthorMar 19.2005 — Actually, i want the click to provoke an eval function, and this :

[code=php]
suite="alert('')"
document.body.onclick=function(){eval(suite)}
[/code]


doesn't work any more (nothing happens, which is not the wanted effect).

Could you let me know the good syntax, please ?

Thanx for the help !
Copy linkTweet thisAlerts:
@MongusMar 19.2005 — The problem is suite is out of scope when the function is called. You need to do something a little tricker like this:

[code=php]
suite="alert('')"
document.body.onclick=eval('function(){eval("' + suite + '")}');
[/code]


You'll have to place your quotes carefully inside of there.

P.S. I didn't test that code specifically but I do this type of thing a lot. Let me know if it doesn't work.
Copy linkTweet thisAlerts:
@7studMar 19.2005 — If you are wanting to use eval(), then you are most likely passing up better alternatives and making your script inefficient in the process.
Copy linkTweet thisAlerts:
@Grand_MamamouchauthorMar 20.2005 — Thanx for all your answers !

The odd thing is that

[code=php]document.body.onclick=function(){...}[/code]

doesn't work, but

[code=php]window.onclick=function(){...}[/code]

does !
Copy linkTweet thisAlerts:
@MongusMar 21.2005 — Sorry for giving you bad code. I don't always test before I post. document.body and window are not the same objects but I'm not sure why you couldn't use document.body.onclick.

According to "Dynamic HTML The Definitive Reference" event handlers for document.body are actually the same as the event handlers for document.
×

Success!

Help @Grand_Mamamouch 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 6.16,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...