/    Sign up×
Community /Pin to ProfileBookmark

Assigning event handlers to HTML objects

I have tried to assing an onclick event to an HTML object, but for some reason the event is ignored. Can anyone advise what’s wrong with the code?

This doesn’t work:

[CODE]<span id=myArea>Click Me</span>
<script language=”javascript”>
document.getElementById(“myArea”).onclick = “alert(‘My Message’)”
</script>[/CODE]

This is what it should be equivalent to (and which works):

[CODE]<span id=myArea onclick = “alert(‘My Message’)”>Click Me</span>[/CODE]

to post a comment
JavaScript

2 Comments(s)

Copy linkTweet thisAlerts:
@BigMoosieJun 10.2005 — &lt;SPAN id=myArea&gt;Click Me&lt;/SPAN&gt;
&lt;SCRIPT type="text/javascript"&gt;
&lt;!--
document.getElementById("myArea").onclick=function(){
alert('My Message');
}
//--&gt;
&lt;/SCRIPT&gt;
Copy linkTweet thisAlerts:
@VicauthorJun 10.2005 — Thanks. Works just nice...
×

Success!

Help @Vic 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.17,
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,
)...