JavaScript
Article
By Sam Deering

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.

--ADVERTISEMENT--

$("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
Recommended
Sponsors
Get the most important and interesting stories in tech. Straight to your inbox, daily.