GETTING NUMBER OF CHARACTERS IN A STRING IN JQUERY



In this post, we will count the number of characters in a string using Jquery. I will show you an example. This blog will go through in detail on finding the number of characters in a string using jquery.

Example 1: We will count Characters from String

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to My Blog</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<h1>Count number of string in Jquery</h1>
<script>
    $(document).ready(function(){
var str = "Welcome to My Blog";
var count = str.length;
console.log(count);
    });
</script>
</body>
</html>

Example2: We will Count Characters

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to My Blog</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<h1>Count number of string in Jquery</h1> 
<input type="text" name="string" value="This is example" class="input1">
<button class="button1">Click</button>
<script>
    $(document).ready(function(){
$('.button1').click(function(){
    var countInput = $('.input1').val().length;
    console.log(countInput);
});
    });
</script>
</body>
</html>

Example3: We Will Count Characters without Space

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to My Blog</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<h1>Count number of string in Jquery</h1>  
<input type="text" name="string" value="This is example" class="input2">
<button class="button2">Click</button>  
<script>
    $(document).ready(function(){
$('.button2').click(function(){
    var countInput = $('.input2').val().replace(/ /g,'').length;
    console.log(countInput);
});  
    });
</script>
</body>
</html>

Comments