jQuery API

jQuery.trim()

jQuery.trim() Returns: String

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

  • version added: 1.0jQuery.trim()

Examples:

Example: Removes the two whitespaces 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>
	<button>Show Trim Example</button>
<script>

    $("button").click(function () {
      var str = "     lots of spaces before and after     ";
      alert("'" + str + "'");

      str = jQuery.trim(str);
      alert("'" + str + "' - no longer");
    });

</script>
</body>
</html>

Demo:

Example: Removes the two whitespaces at the start and at the end of the string.

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

Result:

"hello, how are you?"

Comments

  • Support requests, bug reports, and off-topic comments will be deleted without warning.

  • Please do post corrections or additional examples for jQuery.trim() below. We aim to quickly move corrections into the documentation.
  • If you need help, post at the forums or in the #jquery IRC channel.
  • Report bugs on the bug tracker or the jQuery Forum.
  • Discussions about the API specifically should be addressed in the Developing jQuery Core forum.