JQuery Remove All Unwanted Whitespace From String Example

11-Apr-2023

.

Admin

Hi Guys,

Today,I will explain you how to remove all unwanted 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.

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.

Example


<!DOCTYPE html>

<html lang="en">

<head>

<meta charset="utf-8">

<title>Remove All White Spaces from a Strings</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>

<h3>Original String</h3>

<input id="test" type="text" />

<br>

<h3>Trimmed String</h3>

<pre class="trimmed"></pre>

</body>

</html>

It will help you...

#Jquery