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);
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'));
document.getElementById("password").maxLength
To access it with Javascript you need an uppercase 'L'
The accepted answer is actually wrong:
document.getElementById("password").maxLength
Will get you the maxLength
attribute eg maxLength="2"
, to get the value you must get just that:
document.getElementById("password").maxLength.value /* <-- note .value */
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.