can we put two onclick event in javascript within one input type button tag for calling two different functions?
+1
A:
there is no need to have 2 function within one element, you need just one that call other 2!
<a href="#" onclick="my_func()" >click</a>
function my_func() {
my_func_1();
my_func_2();
}
aSeptik
2010-05-21 10:36:57
+1
A:
You can attach a handler which would call as many others as you like:
<a href="#blah" id="myLink"/>
<script type="text/javascript">
function myOtherFunction() {
//do stuff...
}
document.getElementById( 'myLink' ).onclick = function() {
//do stuff...
myOtherFunction();
};
</script>
Jacob Relkin
2010-05-21 10:39:48
thanks buddy this solution help me more
picnic4u
2010-05-21 14:05:17
+1
A:
This one works:
<input type="button" value="test" onclick="alert('hey'); alert('ho');" />
And this one too:
function Hey()
{
alert('hey');
}
function Ho()
{
alert('ho');
}
.
<input type="button" value="test" onclick="Hey(); Ho();" />
So the answer is - yes you can :) However, I'd recommend to use unobtrusive JavaScript.. mixing js with HTML is just nasty.
rochal
2010-05-21 10:40:23
A:
You could try something like this as well
<a href="#" onclick="one(); two();" >click</a>
<script type="text/javascript">
function one(){
alert('test');
}
function two(){
alert('test2');
}
</script>
Roland
2010-05-21 10:43:33
A:
The HTML
<a href="#" id="btn">click</a>
And the javascript
// get a cross-browser function for adding events, place this in [global] or somewhere you can access it
var on = (function(){
if (window.addEventListener) {
return function(target, type, listener){
target.addEventListener(type, listener, false);
};
}
else {
return function(object, sEvent, fpNotify){
object.attachEvent("on" + sEvent, fpNotify);
};
}
}());
// find the element
var el = document.getElementById("btn");
// add the first listener
on(el, "click", function(){
alert("foo");
});
// add the second listener
on(el, "click", function(){
alert("bar");
});
This will alert both 'foo' and 'bar' when clicked.
Sean Kinsey
2010-05-21 11:04:38