jQuery.trim() in jQuery

jQuery.trim() function is used to remove  the white space from the beginning and end of a string

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

<!DOCTYPE html>
<html>
<head>
  <script src="/scripts/jquery-1.4.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>

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

$.trim("  Hi, where are you going?  ");

Result:

"Hi, where are you going?"

Leave a Reply

Your email address will not be published. Required fields are marked *