jQuery.trim()S2C Home « Utilities « jQuery.trim()

String trimming.

Description

The jQuery.trim() jQuery General utility method, removes whitespace from the beginning and end of a string.

Shorthand version $.trim()

  • Only removes whitespace from start and end of strings.

Syntax


Signature Description
jQuery.trim( str )Remove whitespace from the beginning and end of a string.

Parameters


Parameter Description Type
strThe string to trim.String

Return

A String object.

jQuery.trim( str ) Examplego to top of page Top

Remove whitespace from the beginning and end of a string.

In the example below when we press the button the first time we create a variable and display it before and after trimming within some preformatted text.



$(function(){
  $('#btn3').one('click', function(){
    var ourString = '  A stitch in time saves nine.   ';
    $('#div3').append( '<pre>+++' + ourString + '+++' + '</pre>'<br />');
    $('#div3').append( '<pre>+++' + $.trim( ourString ) + '+++' + '</pre><br />');
  });
});

div3. Some initial text.

Press the button below to action the above code:


go to home page Homepage go to top of page Top