The jQuery.trim() method
Example
Remove leading and trailing whitespace from a string
$
(
function
(
)
{
var
str
=
"
lots of spaces before and after
"
; $
(
"
#original
"
)
.
html
(
"
Original String: '
"
str
"
'
"
)
; $
(
"
#trimmed
"
)
.
html
(
"
$.trim()'ed: '
"
- $.
trim
(
str
)
"
'
"
)
;
}
)
Try it »
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; you can use the native JavaScript String.prototype.trim instead.
Note:The $.trim() function removes all leading and trailing line breaks, spaces (including consecutive spaces), and tabs from a string. If such whitespace characters appear in the middle of a string, they are preserved and not removed.
Syntax
$
.trim( str )
| Parameters | Description |
|---|---|
| str | String type: The string whose leading and trailing whitespace is to be removed. |
jQuery Miscellaneous Methods