jQuery Strip Whitespace

Simple jQuery code snippet to strip whitespace from html elements such removing front and trailing whitespace from text in a div (it doesn’t remove space inside text though). Also see Remove html tags.

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

str = jQuery.trim(str);
alert("'" + str + "' - no longer");
Login or Create Account to Comment
Login Create Account