How to use queryselector in an input?

Asked

Viewed 396 times

1

Hello, I need to select the value that is inside an input, the value is variable from 1 to 12, but I do not know how to do it, here is the input HTML:

<input type="text" id="mudarUnidadePacote" class="input-quantity ng-pristine ng-valid ng-not-empty ng-touched" value="0" ng-model="ctrl.boxQuantity" ng-blur="ctrl.updateBoxQuantity()" packsize="12">

This is my code (it appears that I was able to select the element but the number does not appear, between 0 and 12).

let quantidade = document.getElementById("mudarUnidadePacote").textContent;
alert(quantidade)
  • 1

    Vinicius, textContent picks up a text node that is the child of input. However inputs do not have children because they are not containers. What you want is the property value of InputHTMLElement

  • And it would be nice if you [Edit] your question and add the tags of the frameworks you are using because ng-model is not part of the default HTML and can completely change the most advised answer for your case.

  • @fernandosavio I beg your pardon, my first question here in the stack, in case the framework is Angular.

1 answer

2

The textContent is used to get the text node that can be inside an HTML element.

To get the value of a input, select or textarea, you must use the property value.

Thus:

const field = document.querySelector('#my-field');

field.addEventListener('input', () => {
  // Obtemos o valor:
  console.log(field.value);
});
<input id="my-field" />

Browser other questions tagged

You are not signed in. Login or sign up in order to post.