How to call HTML attribute 'minlength' in JavaScript

Viewed 126

I have this code snippet:

<div>
      <label for="requiredSize">Required Size 5 : </label>
      <input type="text" name="requiredSize" id="requiredSize" class="required_size" minlength="5" />
</div>

I am doing a form validation exercise, and I need to have one of the requirements be based on required size. How can I call the minlength into my JavaScript file to be used, since it does change throughout the form?

I have tried document.querySelector("minlength") to try and save it as a variable, but it wasn't being recognized.

1 Answers

const input = document.querySelector("#requiredSize");
console.log(input.getAttribute('minlength'))
<div>
      <label for="requiredSize">Required Size 5 : </label>
      <input type="text" name="requiredSize" id="requiredSize" class="required_size" minlength="5" />
</div>

Related