jQuery API

jQuery.trim()

jQuery.trim( str ) Returns: String

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

  • version added: 1.0jQuery.trim( str )

    strThe 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.

Examples:

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

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  
        <pre id="original"></pre>
        <pre id="trimmed"></pre>
      
<script>
  var str = "         lots of spaces before and after         ";
  $("#original").html("Original String: '" + str + "'");
  $("#trimmed").html("$.trim()'ed: '" + $.trim(str) + "'");
</script>

</body>
</html>

Demo:

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

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

Result:

"hello, how are you?"

Support and Contributions

Need help with jQuery.trim() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to jQuery.trim()? Report it to the jQuery core team.

Found a problem with this documentation? Report it to the jQuery API team.

* All fields are required
  • http://karlieann.com/ Karlie

    Does this remove anything other than the ordinary space character? What about carriage returns, tab characters, etc? ASCII/UTF8/etc control characters? It would be nice to see a list of exactly which character(s) this function removes.

  • DCP

    trim does not work with IE7 and non breaking spaces. Try using this.
    $(object).text().replace(/(^[sxA0]+|[sxA0]+$)/g, ”); Where $(object).text = test 

  • http://twitter.com/brainphat Tron

    If one wanted to add this function to the String prototype, this works like a champ:

    if(typeof String.prototype.trim !== ‘function’) {
    String.prototype.trim = function() {
    return this.replace(/^s+|s+$/, ”);
    }
    }

    http://stackoverflow.com/questions/2308134/trim-in-javascript-not-working-in-ie

    • anish_m

      use .replace(/^s+|s+$/g, “”)

      global flag should be there

  • Yader Hernandez

    Trim does not work in ie7 or ie8.

    $(this).val().trim() == ''

    Will throw an error: object doesn't support this property or method.

    • Chris

      Try this instead:
      $.trim($(this).val()) == ''

  • Philipp

    Had some interesting problem with this.

    Solution that did NOT work in IE7:
    $('#keywords').val().trim();

    Solution that works in IE7:
    var searchTerm = $('#keywords').val();
    searchTerm = jQuery.trim(searchTerm);

  • http://twitter.com/nzipsi Andrew Thorburn

    Discovered a bug on iPad's Safari: While most browsers can handle calling Trim without a parameter, it appears that Safari on the iPad cannot. Thus,

    $(text).trim() does not cause any issues with most browsers, it makes Safari have a little cry. I'm not actually sure if that works or not, but at least it doesn't result in an error. To make Safari not complain, it should be:

    $.trim(text) instead.

    • http://www.learningjquery.com/ Karl Swedberg

      If you'd like to file a bug report, you can do so at http://bugs.jquery.com/newticket

      Thanks!

      • http://twitter.com/snehesh_mitra Snehesh Mitra

        Yes this issue is there till Safari 4.x but is solved in 5.x onwards. Unfortunately the iphones and the ipads still use 4.x in most of the cases

    • Spamophobe

      $(text).trim() seems to make ie and ff puke on a pc, whereas it works fine in everything i can throw at it from a mac.

  • varun

    It doesnt remove newline characters from the end of the string.

  • Keno

    i cant get trim to work on my form field.. here is my code
    $.trim($('#field').val()).
    help please

  • Keno

    i cant get trim to work on my form field.. here is my code
    $.trim($('#field').val()).
    help please

  • Umamahesh85 M

    The above statement i used and working fine in both IE and FF.
    The only problem in FireFox is it is unable to replace the multi-spaces inbetween the words.
    The leading and tailing spaces are removed..
    Can anyone suggest me how to replace the MULTIPLE SPACES inbetween the words to SINGLE SPACE…

    • daan

      That's a 'normal' string operand, named replace, with a regex that singles out spaces: “your string”.replace(/s+/gi,' ');

      What we're doing here is: selecting one or more spaces (/s+) and replace them with one space.