In this tutorial, learn how to remove all white spaces from text using jquery. The short answer is: use
replace() of jQuery and mention the space you want to remove. The string may contain extra spaces which you can remove easily with this method.
After removal of all the space from the string. You will get a string whose words are closely placed without single space.
This may not prefer in some situation but helpful in some places. Where you don’t want a single piece of space in the text.
Remove All White Spaces From Text Using jQuery
To remove the white spaces from a string, you have to use jQuery text() first. After you get the string in a variable, you have to use replace() to remove all white spaces.
var myTextAll = $('.mystring').text();
var myRemoveSpaceAll = myTextAll.replace(/ /g,'');
$('.allremovetxt').html('<strong>The new string is: </strong>'+myRemoveSpaceAll);
<p class="mystring">This is my string.</p>
<button type="button" class="mybtn btn btn-primary">Remove All White Space</button>
This is my string.
The above example contains a piece of text and the button with the class name. The string contains spaces after each word. Click the above button to remove all the spaces from text. This generates a new string with no spaces at all.
You may also like read.
- Remove special characters from string using jQuery
- How to remove string spaces and dash using jQuery
- Count string characters length using jQuery
Hope, you like this tutorial on how to remove all white spaces from text using jquery. If you have any query regarding the tutorial, please comment below.
Also tell me, which method you are using to remove all whitespace from a string using jQuery.