Skip to content
Related Articles

Related Articles

Improve Article

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

  • Last Updated : 12 Sep, 2019

The task is to get the length of a string with the help of JQuery. We can find the length of the string by the jQuery .length property. The length property contains the number of elements in the jQuery object. Thus it can be used to get or find out the number of characters in a string.

Syntax:

$(selector).length

Example 1:




<!DOCTYPE html>
<html lang="en">
  
<head>
    <meta charset="utf-8">
    <title>Find Length of a String Using jQuery</title>
  </script>
    
    <script type="text/javascript">
        $(document).ready(function() {
            $("button").click(function() {
                var myStr = $("textarea").val();
                if ($(this).hasClass("with-space")) {
                    var withSpace = myStr.length;
                    alert(withSpace);
                } else if ($(this).hasClass("without-space")) {
                    var withoutSpace = myStr.replace(/ /g, '').length;
                    alert(withoutSpace);
                }
            });
        });
    </script>
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;"
        GeeksForGeeks 
    </h1>
    <h3>Enter Text and Click the Buttons</h3>
    <textarea rows="5" cols="60">
      How do you get the length of a string in JQuery?
  </textarea>
    <br>
    <button type="button" class="with-space">
      Length of string including white-spaces
  </button>
    <br>
    <br>
    <button type="button" class="without-space">
      Length of string without white-spaces
  </button>
</body>
  
</html>

Output:
Before Click on the Button:


Example 2:




<!DOCTYPE html>
<html>
  
<head>
    <title>
      Find Length of a String Using jQuery
  </title>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width,
                   initial-scale=1.0">
    <script src=
  </script>
    
    <script>
        $(function() {
            $('#geeks1').keyup(function() {
                var txtlen = $(this).val().length;
                var txtlennospace = $(this).val().replace(/\s+/g, '').length;
                $('#geeks1_space').text(txtlen);
                $('#geeks1_space_no_space').text(txtlennospace);
  
            });
        });
    </script>
  
</head>
  
<body style="text-align:center;">
    <h1 style="color:green;"
        GeeksForGeeks 
    </h1>
    <div style="padding-bottom: 10px;">JQuery - Realtime length</div>
    <div>
        <input style="padding: 7px;" 
               maxlength="60" 
               size="50" 
               type="text"
               id="geeks1" />
        <p>Length with space : 
          <span id="geeks1_space"></span></p>
        <p>Length without space : 
          <span id="geeks1_space_no_space"></span></p>
    </div>
</body>
  
</html>

Output:
Before Entering the Text:

After Entering the Text:




My Personal Notes arrow_drop_up
Recommended Articles
Page :