Categories

jQuery.trim()

Categories: Utilities

jQuery.trim( str )Returns: String

Description: Remove the whitespace from the beginning and end of a string.

  • jQuery.trim( str )

    version added: 1.0

    str   The string to trim.

The $.trim() function removes all newlines, spaces (including non-breaking spaces), and tabs from the beginning and end of the supplied string. If these whitespace characters occur in the middle of the string, they are preserved.

  • Remove the two white spaces at the start and at the end of the string.

    HTML:
    
            <pre id="original"></pre>
            <pre id="trimmed"></pre>
          
    Code:
    
      var str = "         lots of spaces before and after         ";
      $("#original").html("Original String: '" + str + "'");
      $("#trimmed").html("$.trim()'ed: '" + $.trim(str) + "'");
    
  • Remove the two white spaces at the start and at the end of the string.

    Code:
    $.trim("    hello, how are you?    ");
    Results:
    "hello, how are you?"