0

how to know the length of an input in Javascript

alert("Size: "+document.getElementById('champ').length)

not work undefined

2 Answers 2

11
alert("Size: "+document.getElementById('champ').value.length)

You have to pluck the "value" attribute from the DOM element, and get the length of that.

Sign up to request clarification or add additional context in comments.

Comments

1
<input type='text' id='champ' length=3 value="ab">

document.getElementById("champ").getAttribute("length")
//returns 3

document.getElementById("champ").value.length
//returns 2

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.