how to get the maxlength of an input field using javascript

Tech Tech picture Tech Tech · Oct 1, 2013 · Viewed 13k times · Source

I just wanted to know if we could get the maxlength of an input field from javascript

<input type="password" id="password" maxlength="20" >

I tried this but it returns undefined

console.log(document.getElementById("password").maxlength);

Answer

BlitZ picture BlitZ · Oct 1, 2013

Use DOMElement::getAttribute() to obtain properties, that not listed in DOMElement, but existing in markup:

var el = document.getElementById("password");
console.log(el.getAttribute('maxlength'));