/    Sign up×
Community /Pin to ProfileBookmark

Event handler properties Question

Look, here’s what I’m trying to do:

From a hyperlink in the document, and within the onclick attribute of that hyperlink, I’ve tried to loop through all the hyperlinks in the document and then assigning event handler properties to each link from the current hyperlink’s onclick attribute. Hope you got it, in case here’s what it looks like, and note that it doesn’t work, in fact event handler properties don’t seem to work no matter how I try them wether it’s via a function call, or just inline code nothing ever happens. This example didn’t generate any script errors:

[COLOR=red]
<a href=”#” onclick=’for(i=0;i<document.links.length;i++)document.links[i].onclick=”alert(100/45)”‘>link</a>
[/COLOR]

Note that the code doesn’t break in the middle in the original code like it does here. Summary: JavaScript event handler properties won’t work. I’m using Firefox 1.0, and no errors are generated from this example. thanks.

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@KorFeb 23.2005 — Try

onclick="for(i=0;i<document.links.length;i++)document.links[i].onclick=function(){alert(100/45)}"
Copy linkTweet thisAlerts:
@augustauthorFeb 23.2005 — it worked. Great, but how come they say the event handler property's value simply needs to be some JavaScript code in string form, e.g. they don't say that it needs to be a function in particular yet without a function it seems impossible for it to work?


Thanks
Copy linkTweet thisAlerts:
@KorFeb 23.2005 — event handler's "value" (in fact it is not quite a value, it is an object) is always a function, so that the general full syntax is

[i]element.onevent[/i] = function(){foo(params);foo2(params)}

if there is a single function to be fired and if the function has no parameter, the function can be called as an object in itself like

[i]element.onevent[/i] = foo;

The latest syntax is more ofthen to be seen, so that is why you might have believed it as a string variable.
Copy linkTweet thisAlerts:
@KorFeb 23.2005 — Doun't counfound functions with methods... Methods,as well as attributes can be passed/called as variables. Methods can have as parameters functions as string, as, for instance, in wellknown pair setTimeout()/clearTimeOut()... Myabe you were thinking of methods when trying to fire a function as a string...
×

Success!

Help @august 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.19,
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,
)...