Reputation: 1344
I have a onclick function like this
onclick = function(){alert('hello')}
I want to add an additional function to it base on the server response so that I can display more, like not only display hello but also 'you are welcome', how to writ this without jquery?
onclick = function(){alert('you are welcome')}
thank you for helping.
Upvotes: 1
Views: 3728
Reputation: 94319
Use addEventListener
.
elementSelected.addEventListener("click", function(){
alert('you are welcome');
});
This is a modern way to do that.
For that old IE, you have to do this instead:
elementSelected.attachEvent("onclick", function(){
alert('you are welcome');
});
So a more convenience way to do that is
function addEvent(ele, type, func){
if(ele.addEventListener){
ele.addEventListener(type, func, false);
}else if(ele.attachEvent){
ele.attachEvent("on" + type, func);
}
}
addEvent(document.body, "click", function(){
alert("Hello world!");
});
Upvotes: 5
Reputation: 10747
Using event listner's might do the trick ,but please note that the W3C model does not state which event handler is fired first.
I would better suggest you to check for the server response from within your function and call the second function from there.
Upvotes: 1
Reputation: 7954
yourelement.attachEvent('onclick',hello);
Function hello(){
// do wat you want here
}
Upvotes: 0
Reputation: 11122
Without jQuery, add event listeners instead of using the onClick values. Here's a description.
Upvotes: 0