jQuery trim() method

The trim() method is used to remove the space, tabs, and all line breaks from the starting and end of the specified string. This method does not remove these characters if these whitespace characters are in the middle of the string.

The commonly used syntax of using this method is given as follows.

Syntax

The trim() method accepts single parameter str, which is defined as follows.

str: It is a string that is required to be trimmed.

Example

In this example, first, we have to click the given button. After clicking the button, an alert box will be displayed showing the original string. On closing the corresponding alert box, another alert box will be displayed that shows the trimmed string.

Here, the original string contains multiple new lines, tabs, and spaces at the beginning, as well as at the end of the string.

Test it Now

Output:

After the execution of the above code, the output will be -

jQuery trim() method

After clicking the given button, the output will be -

jQuery trim() method

On closing the alert dialog box, we will get the trimmed string as shown below:

jQuery trim() method