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()" />
        function GetLength() {
            var text = document.getElementById("txtName").value;

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.


 Views: 1372 | Post Order: 42

Write for us