1

I have code like

<a id='lnk1' onclick='do something' >test</a>

Later on code is added to the same anchor tag like

lnk = document.getElementById('lnk1')
lnk.onclick = function() { do something}

Now what is happening is that in the second piece of code the onclick function of the anchor tag is getting overwritten. What I want to happen instead is that the first onclick's code is run and after that the 2nd onclick's is run.

4
  • Look at addEventListener. Commented Sep 14, 2011 at 13:17
  • Not an exact duplicate, but related enough to be helpful: stackoverflow.com/questions/2790583/… Commented Sep 14, 2011 at 13:18
  • @Baszz no, that won't work. The browser will turn the string value of the attribute into a full-blown JavaScript function, so appending additional code in string form just makes no sense. Commented Sep 14, 2011 at 13:20
  • @Pointy: Okay, thanks...it was just a thought...wasn't sure. Thanks! Commented Sep 14, 2011 at 13:21

4 Answers 4

3

There is a very simple, standards-compliant way to do this:

lnk1.addEventListener('click', function() {
    // do something
});

This doesn't work in IE before version 9, so you'll need to do this:

var handler = function() {
    // do something
};

if ("addEventListener" in lnk1) { // standards-compliant browsers
    lnk1.addEventListener('click', handler);
} else { // Internet Explorer < v9
    lnk1.attachEvent('onclick', handler);
}

This will work, and both the original function specificed in the HTML attribute and in the code above will run. HOWEVER it would be far nicer to define all your event handlers in the same place: in the Javascript. Think hard about removing event handling logic from your HTML attributes.

Sign up to request clarification or add additional context in comments.

Comments

3

You could try this:

var lnk = document.getElementById('lnk1'); // don't forget var!

var oldHandler = lnk.onclick;
lnk.onclick = function(ev) {
  if (oldHandler) oldHandler(ev);
  // do something ...
};

That code saves a reference to the old handler, and if it's not empty it calls it before doing whatever else the new handler wants to do.

You could put the call to the old handler after the new code, or mixed in, or whatever.

4 Comments

@S.Serpooshan yes, if this is important then you'd want to use .call() to make sure it's correct. Also not that this answer is over 10 years old.
but this way has a potential problem: if the this keyword is used in oldHandler, it will be wrong! one solution is to write: if (oldHandler) oldHandler.call(this, ev);
@S.Serpooshan yes, re-read my comment above.
oh, i just edit my first comment before see your answer comment...
1
<a id='lnk1' onclick='do something' >test</a>

JavaScript

 lnk1 = document.getElementById('lnk1')
    lnk1.addEventListener('click',function() { do something}, false);

when setting onclick you are overwrite existing attribute, but assign click through event listener then it will be ok.

Comments

0

You have a mistake in your statement:

JAVASCRIPT

lnk = document.getElementById('lnk1')
lnk1.onclick = function() { do something} \\ replace lnk1 with lnk
lnk.onclick = function() { do something} \\ this will work

You have defined lnk as variable but your are calling lnk1 with onclick event. This is a wrong statement.

USE HTML

<a id='lnk1'>test</a>

See the Demo: http://jsfiddle.net/tm5cX/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.