I have the following problem:
I have a field that places a value in a input
, and it looks at whether the value placed inside is within the switch
that is in JavaScript . It is giving one the following error:
Uncaught TypeError: Can not read property 'value' of null.
The code is as follows:
<input type="text" name="" id="nome" value="">
<button type="button" onclick="myfunction() ">Aperte</button>
<p id="pnome"></p>
var fruta = document.getElementById("nome").value;
function myfunction(){
switch (fruta) {
case "Laranjas":
document.write("As laranjas custam R$0,59 o kg");
break;
case "Maçãs":
document.write("As maçãs custam R$1,50 o kg");
break;
case "Bananas":
document.write("As bannas custam R$3,00 o kg");
default:
document.write("Desculpe, estamos sem nenhuma " + fruta + ".");
}
}
document.getElementById("pnome").innerHTML = "nome: " + fruta;