Change onClick for an anchor tag

D

delraydog

I'm using:

document.getElementById("mylink").childNodes[0].nodeValue = "New
Text";

To change the text value of a link that I've created.

How can I do something similar to change the onclick event?

(doesn't work)
document.getElementById("mylink").onClick="alert('test')";

Thanks,
Cliff.
 
R

RobB

I'm using:

document.getElementById("mylink").childNodes[0].nodeValue = "New
Text";

To change the text value of a link that I've created.

How can I do something similar to change the onclick event?

(doesn't work)
document.getElementById("mylink").onClick="alert('test')";

Thanks,
Cliff.

It's 'onclick' (lower-case) for one thing.

You need to assign either a actual function (object) or a pointer:

document.getElementById("mylin­k").onclick=function(){alert('test');};

You could, naturally, just use document.createElement('a') to make a
new Link, set its attributes, and call .replaceChild().
 
P

prasad83.a

You will need to assign a function object to onclick event handler:
document.getElementById("mylink").onclick = function() {
alert("onclick"); } works fine but onclick can send implicit Event
object to the handler which can be quite useful. So you can also set
onclick like: document.getElementById("mylink").onclick =
function(event) { alert(event.type); }
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top