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.


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

str = jQuery.trim(str);
alert("'" + str + "' - no longer");
The most important and interesting stories in tech. Straight to your inbox, daily. Get Versioning.
Login or Create Account to Comment
Login Create Account