views:

16881

answers:

7

How do you get the length of a string in jQuery?

+21  A: 

You don't need jquery, just use yourstring.length. See reference here and also here.

Artem Barger
I think you meant to type "You don't need jquery..."
mgroves
You completely right.
Artem Barger
Why on earth I was down voted for my answer?
Artem Barger
I was too, oh the joys of SO.
karim79
At least people could leave an explanation, what was wrong.
Artem Barger
I bet that's just competition. :D
Arnis L.
+2  A: 

It's not jquery you need, it's JS:

alert(str.length);
karim79
+2  A: 

same way you do it in javascript:

"something".length

mkoryak
+3  A: 

You don't need to use jquery.

var myString = 'abc';
var n = myString.length;

n will be 3.

Lawrence Barsanti
+4  A: 

jQuery is a JavaScript library.

You don't need to use jQuery to get the length of a string because it is a basic JavaScript string object property.

somestring.length;
legenden
+1 for including the line 'jQuery is a JavaScript library.' Lots of answers are saying you don't need jQuery, but this may be confusing to a person who thinks that 'jQuery' and 'JavaScript' are two different things.
Grant Wagner
+6  A: 

The easiet way: $('#selector').val().length

Andrei Iarus