JQuery string length

10.4k views Asked by At

I try to find the length of a string with jquery but it doesn't work...

Here a JsFiddle : http://jsfiddle.net/3LtjdkLw/1/

HTML code:

<div id="signup-pwd">ABC</div>

Javascript code:

alert($('#signup-pwd').val().length)

Thank you!

2

There are 2 answers

0
David Thomas On BEST ANSWER

Because a <div>, unlike <input>, <select> and <textarea> elements, has a textContent/innerText property rather than value, so you should use text() instead of val():

$('#signup-pwd').text().length;

Updated JS Fiddle demo.

References:

0
Lim H. On

You need .text() not .val() to get the text value inside a textual HTML element.