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
3 odpowiedzi
+ 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)
+ 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
- 1
Change js code to this
function convert() {
var miles = document.getElementById("miles").value;
var kilometer = miles*1.609344;
document.getElementById("demo").innerHTML = kilometer;
}