views:

141

answers:

5

Hi,
how can i add more behaviour to existing onclick events e.g. if the existing object looks like

<a href="http://abc" onclick="sayHello()">link</a>
<script>
function sayHello(){
    alert('hello');
}

function sayGoodMorning(){
    alert('Good Morning');
}
</script>

how can i add more behavior to the onclick that would do also the following

alert("say hello again");
sayGoodMorning()

Best Regards, Keshav

A: 

One way would be to write a third function:

<a href="http://abc" onclick="foo()">link</a>
<script>
function sayHello(){
    alert('hello');
}

function sayGoodMorning(){
    alert('Good Morning');
}

function foo() {
    alert("say hello again");
    sayGoodMorning();
}
</script>
Darin Dimitrov
A: 

<a href="http://abc" onclick="sayHello(),sayX(),sayY(),sayZ()">link</a>

would also work

sushil bharwani
+3  A: 

Here's the dirtiest way :)

<a href=".." onclick='sayHello();alert("say hello again");sayGoodMorning()'>.</a>

Here's a somewhat saner version. Wrap everything into a function:

<a href=".." onclick="sayItAll()">..</a>

JavaScript:

function sayItAll() {
    sayHello();
    alert("say hello again");
    sayGoodMorning();
}

And here's the proper way to do it. Use the event registration model instead of relying on the onclick attribute or property.

<a id="linkId" href="...">some link</a>

JavaScript:

var link = document.getElementById("linkId");
addEvent(link, "click", sayHello);
addEvent(link, "click", function() {
    alert("say hello again");
});
addEvent(link, "click", sayGoodMorning);

A cross-browser implementation of the addEvent function is given below (from scottandrew.com):

function addEvent(obj, evType, fn) {
    if (obj.addEventListener) {
        obj.addEventListener(evType, fn, false);
        return true;
    } else if (obj.attachEvent) {
        var r = obj.attachEvent("on" + evType, fn);
        return r;
    } else {
        alert("Handler could not be attached");
    }
}

Note that if all 3 actions must be run sequentially, then you should still go ahead and wrap them in a single function. But this approach still tops the second approach, although it seems a little verbose.

var link = document.getElementById("linkId");
addEvent(link, "click", function() {
    sayHello();
    alert("say hello again");
    sayGoodMorning();
});
Anurag
+1 - the only thing I would change would be to remove the `useCapture` from the `addEvent` implementation to keep consistency across all browsers
Russ Cam
Hi Anurag,i wanted to add the new behavior with javascript i.e. to get the existing script for onclick event and attach more behavior to it.i could have added multiple onclick listeners but IE only has attachEvent and it does not support addEvent or addEventListeners which could have been used to add multiple event listeners
keshav.veerapaneni
@Russ - good idea, will remove `useCapture`.
Anurag
@keshav - the `addEvent` function in the answer is cross-browser and should take care of IE/other browsers.
Anurag
@Anurag - yes but the attachEvent overwrites my existing script for onclick what i want to do is really to extend or to say add more event handlers for same event type. russ has proposed a solution below to achieve it using anonymous functions.
keshav.veerapaneni
@keshav - are you **sure** `attachEvent` overwrites the `onclick` that is added inline in html?
Anurag
@Anurag: i tried some sample script http://jsfiddle.net/z3yjh/6/and yes you are right attachEvent appends to the old script. somehow when i tried some samples b4 posting the question i was not able to get both of them working and thought that attachEvent clears the oldscript.thanks for explanation and quick comments :)
keshav.veerapaneni
A: 

Assuming a slight change to your code:

<a href="http://abc" id="a1" onclick="sayHello()">link</a>

In plain ol' JavaScript, you'd do something like this.

var a = document.getElementById('a1');
a.onclick = function () { alert('say hello again'); a.onclick(); }

It's worth noting that jQuery makes this a bit easier. See the documentation on the click, bind, and one, for example, and in general the section on event handler attachment.

Weston C
+2  A: 

Another way not mentioned is to capture the function currently assigned to the element.onclick attribute, then assign a new function that wraps the old one. A simple implementation to demonstrate would be something like

function addEvent(element, type, fn) {
    var old = element['on' + type] || function() {};
    element['on' + type] = function () { old(); fn(); };
}

var a = document.getElementById('a');

function sayHello(){
    alert('hello');
}

function sayGoodMorning(){
    alert('Good Morning');
}

addEvent(a, 'click', sayHello);
addEvent(a, 'click', sayGoodMorning);

Working Demo here

Russ Cam
thanks, this was what i needed(to extend the existing behaviour with javascript, the addevent method above does it very well) also very thanks for introducing me to jsfiddle looks like a great debugging and sharing tool
keshav.veerapaneni