JQuery string length

10.5k 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 best solutions below

0
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
On

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