/    Sign up×
Community /Pin to ProfileBookmark

link not being updated?

I want to add an onClick-event to a link I have, via a function.
It gets added, but the functionality does not appear to be working.
Any idea why?

function populateLinks(changeLink) {
document.getElementById(changeLink).onClick = “anotherFunction();”
}

<body id=”body”>
<a href=”#” onClick=”populateLinks(‘linkToBeChanged’)”>Populate the links</a><br>
<a href=”#” id=”linkToBeChanged”>My link</a>
<br><br>
<a href=”#” onClick=”alert(document.getElementById(‘body’).innerHTML);”>Click here to check that it is added</a>
</body>

// Michelle

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@Khalid_AliSep 03.2003 — I don't think you can add a synthetic event with that.Please refer to MS / MOzilladocumentation for the addition of synthetic events
Copy linkTweet thisAlerts:
@michelleauthorSep 04.2003 — Actually, I found out how to do it. Just add this at the end of the function.

if(document.all){

document.body.innerHTML = document.body.innerHTML;

}

I have another question though.

Can I remove it, or can I just set it to empty, like height=""?

// Michelle
Copy linkTweet thisAlerts:
@DOSPromptSep 04.2003 — When adding functions to event handlers, you have to set event to the actual function, not a string. So you need to do the following...
document.getElementById(changeLink).onclick = anotherFunction;
Note that the brackets are not included when setting the function reference. If you do, then it assumes you want to set the event handler to the output of the function, rather than the function itself. Also, I think you have to use lower case rather than camel case for the event handler name (though I'm not 100% certain about that).

Hope this helps.
×

Success!

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