0

If i have assigned two functions in one button and i then called each function. Why is the second function only working?

30th Jan 2017, 10:41 AM
Claire
6 Antworten
+ 9
The first one was Overridden.
30th Jan 2017, 11:17 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 9
I don't see 2 functions……
30th Jan 2017, 11:36 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 8
Give me your code.
30th Jan 2017, 11:21 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 8
Put them back and I'll tell you how to fix it.
30th Jan 2017, 11:39 AM
Valen.H. ~
Valen.H. ~ - avatar
+ 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 }
30th Jan 2017, 12:06 PM
Valen.H. ~
Valen.H. ~ - avatar
+ 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 ;)
31st Jan 2017, 8:20 AM
visph
visph - avatar