JavaScript > Methods

Length of string in JavaScript

How to get the length of the string in JavaScript?


In case we want to know the length of the string entered into the textbox in JavaScript, we can follow this approach.

Enter name:
    <input type="text" id="txtName" name="txtName" />
    <input type="button" id="btnsm" value="Get Length" onclick="GetLength()" />
    
    <script>
        function GetLength() {
            var text = document.getElementById("txtName").value;
            alert(text.length);
        }
    </script>

In the above code snippet, we are getting the value of the textbox (using document.getElementById by passing the id value of the text box) on click of the button in text variable. Then calling the length property to get the length of the string in the alert box.

OUTPUT

 Views: 7366 | Post Order: 42



Write for us






Hosting Recommendations