jQuery

How to remove all spaces from a string with jQuery

In this tutorial, we are going to see how to remove all spaces from a string with jQuery. You can use jQuery’s $.trim() function to remove all spaces (including unbreakable spaces), newlines, and tabs at the start/end of a specified string. However, the spaces in the middle of the string are preserved. The following example will show you how to remove white space at the start/end of a string.
 

How to remove all spaces from a string with jQuery
<!DOCTYPE html>
<html>
	<head>
		<title>Remove all spaces from a string</title>
		<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
		<script type="text/javascript">
			$(document).ready(function(){
				var str = $(".original").text();
				var cut = $.trim(str);
				$(".cut").html(cut);
			});
		</script>
	</head>
	<body>
		<div>
			<h5>Original chain :</h5>
			<pre class="original">      Lorem ipsum dolor sit amet, consectetur adipiscing elit.       </pre>
			<br>
			<br>
			<h5>String without space :</h5>
			<pre class="cut"></pre>
		</div>
	</body>
</html>
Result
Original chain :
      Lorem ipsum dolor sit amet, consectetur adipiscing elit.       


String without space :

mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *