JavaScript
Article

jQuery Strip Whitespace

By Sam Deering

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");
});

No Reader comments

Recommended
Sponsors
Because We Like You
Free Ebooks!

Grab SitePoint's top 10 web dev and design ebooks, completely free!

Get the latest in JavaScript, once a week, for free.