0
If i have assigned two functions in one button and i then called each function. Why is the second function only working?
6 Réponses
+ 9
The first one was Overridden.
+ 9
I don't see 2 functions……
+ 8
Give me your code.
+ 8
Put them back and I'll tell you how to fix it.
+ 8
<DOCTYPE html>
<html>
<body>
<img id= "numbers" scr = This will be the link of photos>
<button onclick = "function(){Allnumbers(); AllLetters();}"Click </button>
<script>
function Allnumbers(){
document.getElementById("numbers").src =This will be another link for photos
}
function AllLetters() {
document.getElementById("numbers").src =This will be another link
}
+ 1
@VH:
Your code isn't working... on click you declare a new anonyme function, but don't execute it :P
You should write instead:
onclick="(function(){Allnumbers(); AllLetters();})();"
But simpliest:
onclick="Allnumbers(); AllLetters();"
... because inlined event handlers are implicit function declaration ( as when you set the attribute through js, you can't set it with a string, but you must do it with a function declaration ^^ )
Other way, best practice, is to use the addEventListener() method of the html element:
window.onload=function(){
var mybtn=document.getElementById('buttonId');
mybtn.addEventListener('click',Allnumbers,false);
mybtn.addEventListener('click',AllLetters,false);
}
By this way, you can add any event listener without overriding ;)