How to remove white spaces from a string using jQuery ? Last Updated : 15 May, 2023 Summarize Comments Improve Suggest changes Share Like Article Like Report In this article, we will see how to remove the white spaces from string using jQuery. To remove the white spaces, we will use trim() method. The trim() method is used to remove the white spaces from the beginning and end of a string. Syntax: jQuery.trim( str ) Parameter: This method accepts a single parameter string that is to be trimmed. In this case, we write string containing white spaces in <pre> tag and then apply trim() method to remove all white spaces from starting and ending position. After removing the white spaces we use html() method to display the trimmed string. Example: This example illustrates the removal of the white spaces from a string using jQuery. HTML <!DOCTYPE html> <html> <head> <title> How to remove white space from a string using jQuery? </title> <script src= "https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"> </script> <script> $(document).ready(function () { $("h1").css("color", "green"); $("button").click(function () { $(".str").html("String without whitespaces"); var str = $(".string").text(); var trimStr = $.trim(str); $(".res-string").html(trimStr); }) }); </script> </head> <body> <h1>GeeksforGeeks</h1> <h3> How to remove white space from a string using jQuery? </h3> <h4>String containing white spaces</h4> <pre class="string"> GeeksforGeeks </pre> <button>Trim String</button> <h4 class="str"></h4> <pre class="res-string"></pre> </body> </html> Output: Comment More infoAdvertise with us Next Article How to Allow Only Special Characters and Spaces in Text Field Using jQuery ? V vkash8574 Follow Improve Article Tags : JQuery HTML-Tags jQuery-Methods HTML-Questions jQuery-Questions +1 More Similar Reads How to remove all paragraphs from the DOM using jQuery ? To remove elements and content, we use jQuery remove() method which removes the selected element as well as everything inside it, also it will remove all bound events associated with that target element. Syntax: $(selector).remove() Example : HTML <!DOCTYPE html> <html> <head> < 1 min read How to check a string starts/ends with a specific string in jQuery ? JavaScript provides a lot of string methods that check whether a string is a substring of another string. So, jQuery wouldn't be necessary at all to perform this task. However, we will cover all the different ways of checking whether a string starts or ends with a string:Â startsWith() and endsWith() 5 min read How to make first word bold of all elements using jQuery ? In this article, we will make first word bold of all elements using jquery. To make the first word bold, we use html(), and text() methods. html() Method: The html() method in jQuery is used to set or return the innerHTML content of the selected element. Syntax: It sets the content of matched elemen 1 min read How to find sub-string between the two words using jQuery ? Given a string containing words and the task is to find substring between two given words using jQuery. There are two methods to solve this problem which are discussed below: Using match() method: It searches a string for a match against any regular expression and if the match is found then it retur 2 min read How to Allow Only Special Characters and Spaces in Text Field Using jQuery ? We are going to create an input box that only contains the special characters and the space as the input. It will show an error if any other character is entered. We will use regular expressions that consist of only special symbols and spaces with different events in jQuery. Table of Content Using r 3 min read How to remove all attributes of an HTML element using jQuery ? In this article, we will see how to remove all attributes of an HTML element using jQuery. To remove all attributes of elements, we use the removeAttributeNode() method and .removeAllAttributes(). Syntax: $.fn.removeAllAttributes = function() { return this.each(function() { $.each(this.attributes, f 1 min read Like