How to Use Len() in JavaScript
Most programming languages have a "Len" function to determine the number of characters contained in a string variable. JavaScript does not have a preprogrammed Len function, but you can create one yourself for your website. The Len function calls the "length" function to count the number of characters and it returns the integer value to the code that calls the function.
Instructions
-
-
1
Right-click the HTML file you want to use to call the Len function. Select "Open With," then click the HTML editor you want to use. Click "Open" to open the editor and the HTML code.
-
2
Create the "Len" function. You must type JavaScript code between the opening "<script>" tag and the closing "</script>" tag. Type the following code to create the function:
function Len(string_variable)
{
return string_variable.length;
}
-
-
3
Call the Len function from your HTML code. For instance, the following code calls the "Len" function after you click a button:
<input type="button" onClick="Len(document.getElementById('first_name').value)">
The code above counts the number of characters in the "first_name" text box in the HTML file. Replace "first_name" with the field value you want to count.
-
4
Press the "Ctrl" and "S" keys to save the HTML code. You can test the new code in a Web browser installed on the computer.
-
1