263

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

Sebastian Simon
  • 18,263
  • 7
  • 55
  • 75
RubbleFord
  • 7,456
  • 9
  • 50
  • 80

10 Answers10

499

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
}
Artem Barger
  • 40,769
  • 9
  • 59
  • 81
212

The easiest way:

$('#selector').val().length
StudioTime
  • 22,603
  • 38
  • 120
  • 207
Andrei Iarus
  • 2,329
  • 1
  • 14
  • 4
44

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;
andreialecu
  • 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
22

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;
A_funs
  • 1,228
  • 2
  • 19
  • 31
  • 1
    It'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
22

HTML

<div class="selector">Text mates</div>

SCRIPT

alert(jQuery('.selector').text().length);

RESULT

10

Alan Whitelaw
  • 16,171
  • 9
  • 34
  • 51
Joedel
  • 221
  • 2
  • 2
21

You don't need to use jquery.

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

n will be 3.

Lawrence Barsanti
  • 31,929
  • 10
  • 46
  • 68
14

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

alert(str.length);
karim79
  • 339,989
  • 67
  • 413
  • 406
13

same way you do it in javascript:

"something".length

mkoryak
  • 57,086
  • 61
  • 201
  • 257
4

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

Maxim Saplin
  • 4,115
  • 38
  • 29
2

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;
David Faber
  • 12,277
  • 2
  • 29
  • 40
Mahak Choudhary
  • 1,286
  • 1
  • 16
  • 13