+ 1
Why this code doesn't work 😖(web)
I am trying to create a calculator that finds velocity using u+at=v formula but I am facing issues the calculate button is not showing the result https://code.sololearn.com/WDjx1fr0GFoW/?ref=app
4 Réponses
+ 1
https://code.sololearn.com/WYW4JmURKZY7/?ref=app
just see this we need document.getElementsByClasname("")[0].value since it might return the array so instead if u use document.getElementById("") u would get correct result.
+ 1
Your explanation is good but it does not work I think .It gives error Arvind Singh Rawat.
0
Issues:
1. Your js is executing before your DOM is rendered.
Hence, no element is found.
2. Always use getElementById() to get/set values, because of unique target, and for CSS manipulation use getElementsByClass() because you may need to edit multiple classes simultaneously.
3. Always consider undefined cases.
function find(){
var a=document.getElementById("ac").value;
var iv=document.getElementById("u").value;
var ti=document.getElementById("t").value;
a= a?a:0;
iv= iv?iv:0;
ti= ti?ti:0;
var velocity= iv+a*ti;
console.log(velocity);
}
0
Ohh, I forgot to mention, add corresponding id's in html as classes.
For example:
change class= "ac" to id="ac"
similarly, for ti and u