Easy Tutorial
❮ Misc Get Html Prependto ❯

jQuery.trim() Method

jQuery Miscellaneous Methods

Example

Remove whitespace from the beginning and end of a string

$(function () { 
    var str = "         lots of spaces before and after         ";
    $( "#original" ).html( "Original String: '" + str + "'" );
    $( "#trimmed" ).html( "$.trim()'ed: '" + $.trim(str) + "'" );
})

Definition and Usage

The $.trim() function is used to remove whitespace characters from both ends of a string.

The $.trim() function is deprecated in jQuery 3.5 and above. It can be replaced with the native JavaScript String.prototype.trim.

Note: The $.trim() function removes all newline, space (including consecutive spaces), and tab characters from the beginning and end of the string. If these whitespace characters are within the string, they will be preserved and not removed.

Syntax

Parameter Description
str String type The string that needs to have its leading and trailing whitespace characters removed.

jQuery Miscellaneous Methods

❮ Misc Get Html Prependto ❯