+ 9

Why only one button works? and the other doesn't

why does the script works only button when the window is loaded and without loaded? and How make that the 2 buttons works? code: https://code.sololearn.com/WMqwUANQw1eV/#js Solved, in part. Surged a related quesiton: https://www.sololearn.com/Discuss/403299/where-is-positioned-the-script-inside-html-tab-when-it-is-made-in-js-tab-codeplayground code 2: https://code.sololearn.com/WIUa79ydJJMp/#html

20th May 2017, 4:53 PM
ysraelcon
ysraelcon - avatar
8 odpowiedzi
+ 4
write js file like that : window.onload=function(){ btn_make.onclick=function(){ alert("made, only with window.onload\n and button f_call is not defined, why?"); };//btn_make }; //window.onload function f_call(){ alert("calling, only without window.onload\n and button_make is not defined, why?"); }//f_call
19th May 2017, 4:17 PM
MBZH31
MBZH31 - avatar
+ 2
Place function f_call() outside the window.onload() otherwise it will be out of the scope once it's been loaded.
19th May 2017, 4:04 PM
wmwm
+ 2
Idk if this is correct, but now it's not throwing errors: HTML: <button id="btn_make" onclick="btn_make()">make</button> <button id="btn_call" onclick="f_call()">call</button> JS: function btn_make(){ alert("made, only with window.onload\n and button f_call is not defined, why?"); }
19th May 2017, 4:11 PM
Joseph P French
Joseph P French - avatar
+ 2
What is the position of the "script" inside the html tab, in sololearn codeplayground? because "window.onload" is unnecessary when editing it locally, or placing the script before closing </body> reformulated question: https://www.sololearn.com/Discuss/403299/where-is-positioned-the-script-inside-html-tab-when-it-is-made-in-js-tab-codeplayground
20th May 2017, 4:46 PM
ysraelcon
ysraelcon - avatar
+ 2
@Calvin now, the question is that, when I place it directly inside html, it works, example: https://code.sololearn.com/WIUa79ydJJMp/#html so what is the script's position inside html, when it is in codeplaygorund's JS tab?
20th May 2017, 5:03 PM
ysraelcon
ysraelcon - avatar
+ 1
You cant use id name directly in Javascript btn_make.onclick Use getElementById function And make sure it is called by window.onload.
19th May 2017, 4:22 PM
Calviղ
Calviղ - avatar
+ 1
@ysraelcon Your question keeps on reappeared on recent list. How you did that? With all the suggestions, your issue remind unresolved?
20th May 2017, 4:20 PM
Calviղ
Calviղ - avatar
0
because make hasnt got onclick
19th May 2017, 4:01 PM
Szabó Gábor
Szabó Gábor - avatar