What mistake I have done?? | Sololearn: Learn to code for FREE!
Новый курс! Каждый программист должен знать генеративный ИИ!
Попробуйте бесплатный урок
0

What mistake I have done??

Can anyone please tell me what mistake I have done in this code and why it is not working????? https://code.sololearn.com/WZ0XZvJmDZ95/?ref=app

2nd Sep 2020, 5:31 AM
Aditya Raj
Aditya Raj - avatar
3 ответов
+ 1
Aditya raj You cannot access a function that is declared inside window.onload outside of window.onload. i.e. ------------- window.onload = ()=>{ // function hello declared inside function hello(){ console.log('hello world'); } } hello(); //throws error -------------- My suggestions: Change the onchange attribute of your input like this. ------------- <input onchange="convert(this.value)" > ------------- It provides the value of the input to convert() with its call, no need to manually get the value of input inside convert(). In your particular case, you don't need window.onload at all. Just this convert function will be enough. ------------------ function convert(miles){ miles = Number(miles); let km = miles*1.609; // Showing the kilometres } ------------------ 1. Change the value of the input to a number. i.e. miles = Number(miles)
2nd Sep 2020, 6:05 AM
Hanuma Ukkadapu
Hanuma Ukkadapu - avatar
+ 3
Warp your all Javascript code on window.onload example: window.onload = () =>{ //your Javascript code } so it will run Javascript after window loads means , Javascript can Access all DOM elements after they get rendered
2nd Sep 2020, 5:40 AM
Sudarshan Rai
Sudarshan Rai - avatar
- 1
Change js code to this function convert() { var miles = document.getElementById("miles").value; var kilometer = miles*1.609344; document.getElementById("demo").innerHTML = kilometer; }
2nd Sep 2020, 5:44 AM
Sumit Programmer😎😎
Sumit Programmer😎😎 - avatar