Get the value of a field

0

I'm starting the studies with pure JavaScript, to practice better I created a table as if they were ordering from a pizzeria, and I put some field (taste, quantity and value). At the end of the line I have a "+" button. The idea is that when I click on it, add to the quantity field. For this, I imagined that it would be started this way at least to get this field, but when I try to print it it displays msg of undefined :

//CAPTURA O BOTÃO +
var botaoSoma = document.querySelector(".btnSoma");

// CAPTURA A QUANTIDADE DA PIZZA
var calabresaQtd = document.querySelector("#quantidade_calabresa");


//ACRESCENTA UM EVENTO AO CLICAR NO BOTÃO + E IMPRIME A QUANTIDADE DE PIZZA
botaoSoma.addEventListener('click', function(){
    alert(calabresaQtd.value);    
});
    
asked by anonymous 28.08.2018 / 04:26

2 answers

0

I do not know how your code is html , but you can use getElementById , which picks a certain choice ID or querySelector , which will pick up the selector:

function MyFunction(){
   //Pegar pelo Id
   var id = document.getElementById("inputId").value;
   //Pegar pelo Selector
   var qtd = document.querySelector("#qtd").value;
   alert(id);
   alert(qtd);
}
<!DOCTYPE html>
<html>
<head>
  <title>teste</title>
</head>
<body>
   <form>
      <label>Digite:</label>
      <input type="text" id="inputId">
      <label>Digite:</label>
      <input type="text" id="qtd">
      <input type="submit" value="Enviar" onclick="MyFunction()">
   </form>
</body>
</html>
    
28.08.2018 / 05:41
-5

Use Id instead of class to capture input value

var calabresaQtd = document.getElementById ("calender_quantity");

example: link

    
28.08.2018 / 05:20