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?"