views:

38

answers:

2

Hello, how to know the length of an input in Javascript

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

not work undefined

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

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

Pointy
A: 
<input type='text' id='champ' length=3 value="ab">

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

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