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

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 src="https://code.jquery.com/jquery-1.12.4.min.js">  </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="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js">  </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:
jQuery-Misc
Picked
JQuery
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Form validation using jQuery
How to Dynamically Add/Remove Table Rows using jQuery ?
How to Show and Hide div elements using radio buttons?
Scroll to the top of the page using JavaScript/jQuery
jQuery | children() with Examples
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?