JQuery Remove All Unwanted Whitespace From String Tutorial


Share:
Published By: Admin, Published On: Jul 23, 2021, Category: Jquery

Hello Friends,

Today,in this example I will learn you how to remove all whitespace from string in jquery. We will show example jquery remove all unwanted whitespace from string.You can easy remove all unwanted whitespace from string in jquery.

Here, we will provide a inbuilt jQuery $.trim() function to remove all spaces (including non-breaking spaces), newlines, and tabs from the beginning and end of the specified string. However, the whitespaces that occur in the middle of the string are preserved.

Here, i will give you a full example of how to remove unwanted whitespace from string in jquery.

Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Remove All White Spaces from a Strings - MyWebTuts.com</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
<script type="text/javascript">
    $(function () {
        $('#test').keyup(function () {                     
            var text = $.trim($(this).val());
            $(".trimmed").html(text);
        });
    }); 
</script>
</head>
<body style="background-color: #f7fcff;">
    <h3>Original String</h3>
    <input id="test" type="text" />
    <br>
    <h3>Trimmed String</h3>
    <pre class="trimmed"></pre>
</body>
</html>  
 

It will help you...

Share: