I'm trying to make an app that calculates your weight on other celestial objects. The function does what it should when called with weightConverter(object, weight);.
But I can't seem to get the input from a HTML form.
How can I get the input from the HTML?
Also, is there a better way to getting the result back to the HTML?
function weightConverter(){
var object = document.getElementsByName('object');
var weight = document.getElementsByName('weight');
var objects = {
Sun: 28,
Mercury: 0.38,
Venus: 0.91,
Moon: 0.166,
Mars: 0.38,
Jupiter: 2.14,
Saturn: 0.91,
Uranus: 0.86,
Neptune: 1.1,
Pluto: 0.8
}
var result = weight*objects[object];
document.getElementById("calc").innerHTML= object;
}
<h2 id="calc">....</h2>
<form>
<p> Input weight in kilograms: </p>
<input id="kilo" type="text" name="weight">
<p>Choose object:</p>
<label><input name="object"type="radio" value="Sun">Sun</label>
<label><input name="object"type="radio" value="Mercury">Mercury</label>
<label><input name="object"type="radio" value="Venus">Venus</label>
<label><input name="object"type="radio" value="Moon">Moon</label>
<label><input name="object"type="radio" value="Mars">Mars</label>
<label><input name="object"type="radio" value="Jupiter">Jupiter</label>
<label><input name="object"type="radio" value="Saturn">Saturn</label>
<label><input name="object"type="radio" value="Uranus">Uranus</label>
<label><input name="object"type="radio" value="Neptune">Neptune</label>
<label><input name="object"type="radio" value="Pluto">Pluto</label>
<button onclick="weightConverter();" type="submit">Calculate!</button>
</form>
weightinput element has an identifier and the others don't need one. The radio buttons represent a group no need for an id on each of them,nameworks just fine and is all you need to get an array posted correctly in a POST alsodocument.querySelector('input[name="object"]:checked')works fine. - Generally I only add identifiers as well as other attributes if really needed not for the sake of it.