How do you get the length of a string in jQuery?
-
8Excellent just use normal JS. – RubbleFord Jun 25 '09 at 14:03
10 Answers
You don't need jquery, just use yourstring.length
. See reference here and also here.
Update:
To support unicode strings, length need to be computed as following:
[...""].length
or create an auxiliary function
function uniLen(s) {
return [...s].length
}

- 40,769
- 9
- 59
- 81
-
6
-
11
-
11
-
2Artem Barger - probably because he was looking for the same thing as I was: a way to get the length of text contained in an HTML tag. I'm a beginner so I knew how to get the content but not its length. In that sense, genesis' answer below is better. – Adrian Pauly Jan 11 '16 at 20:14
-
1`"".length == 2`. How can we get the actual number of characters? – Pierre Thibault May 22 '19 at 12:28
-
to make unicode aware lenght you need to run it following way: [...""].length – Artem Barger May 22 '19 at 12:32
-
The easiest way:
$('#selector').val().length

- 22,603
- 38
- 120
- 207

- 2,329
- 1
- 14
- 4
-
39
-
5how is that? no where in the question does he say anything about the string coming from an `:input` value – mkoryak Sep 05 '14 at 01:38
-
2so does this one: `len = $('#selector').is('div') ? "someString".length : 0` and makes just as much sense... – mkoryak Dec 29 '14 at 23:28
-
but this still just uses javascript doesn't it? same as @Artem Barger's answer – Malcolm Salvador Jul 31 '17 at 01:37
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;

- 3,639
- 3
- 28
- 36
-
14+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 Jun 25 '09 at 14:43
A somewhat important distinction is if the element is an input or not. If an input you can use:
$('#selector').val().length;
otherwise if the element is a different html element like a paragraph or list item div etc, you must use
$('#selector').text().length;

- 1,228
- 2
- 19
- 31
-
1It's right Answer for this Question! Here is really defined, how gets a length of "input field" or "text" value. Additional it's possible to get html length, like `$('#selector').html().length`. – Eugen Jan 08 '17 at 16:01
HTML
<div class="selector">Text mates</div>
SCRIPT
alert(jQuery('.selector').text().length);
RESULT
10

- 16,171
- 9
- 34
- 51

- 221
- 2
- 2
You don't need to use jquery.
var myString = 'abc';
var n = myString.length;
n will be 3.

- 31,929
- 10
- 46
- 68
In some cases String.length might return a value which is different from the actual number of characters visible on the screen (e.g. some emojis are encoded by 2 UTF-16 units):
MDN says: This property returns the number of code units in the string. UTF-16, the string format used by JavaScript, uses a single 16-bit code unit to represent the most common characters, but needs to use two code units for less commonly-used characters, so it's possible for the value returned by length to not match the actual number of characters in the string.
In Unicode separate visible characters are called graphemes. In case you need to account for this case, you'll need some lib that can split the string into graphemes, such as this: https://www.npmjs.com/package/grapheme-splitter

- 4,115
- 38
- 29
In jQuery :
var len = jQuery('.selector').val().length; //or
( var len = $('.selector').val().length;) //- If Element is Text Box
OR
var len = jQuery('.selector').html().length; //or
( var len = $('.selector').html().length; ) //- If Element is not Input Text Box
In JS :
var len = str.length;

- 12,277
- 2
- 29
- 40

- 1,286
- 1
- 16
- 13