Search code examples
javascriptinnerhtml

JS: How to print a value from a text box into the screen?


I want to print a value onto the screen based on text in the input (textbox) in the form.

<input type="text" name="pesos" value="">
<button onclick="conversorMonedas()">Convertir!</button>
<p id="resultado"></p>

function conversorMonedas() {
    var pesos = document.getElementById("pesos");
    document.getElementById("resultado").innerHTML = pesos.value;
}

When I click the button, the error at the console appears briefly and then disappears. I managed to read it and it says that pesos.value is null.

How can I print out what I wrote in the text box? Thanks!


Solution

  • You don't have element with id pesos. Add id to your input:

    <input type="text" name="pesos" id="pesos" value="">