Remove Whitespace from String using JQuery

JQuery provides trim method We can use the 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 occurs in the middle of the string are preserved. The trim() method does not change the original string.

Trim function only removes spaces from starting and end of the string. If there are any space between middle of the statement that is ignored.

Below example will show you how to remove leading and trailing whitespaces from a string of text.

<script type=”text/javascript”>
$(document).ready(function(){
var spacestr = ” Hello Webtechsource “;
var withoutspacestr = $.trim(spacestr);
alert(withoutspacestr);
});
</script>

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face – we are here to solve your problems.

I, Dayanand Saini,the founder and chief editor of www.webtechsource.com,have been working as a software engineer from last 4+ years. It's my hobby to learn new things and implement on live environment.

Leave a Reply

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

82 − = 81

%d bloggers like this: