Click to See Complete Forum and Search --> : link not being updated?


michelle
09-03-2003, 09:15 AM
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

Khalid Ali
09-03-2003, 11:10 AM
I don't think you can add a synthetic event with that.Please refer to MS / MOzilladocumentation for the addition of synthetic events

michelle
09-04-2003, 07:24 AM
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

DOSPrompt
09-04-2003, 08:13 AM
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.