Skip to main content

jQuery Strip Whitespace

By Sam Deering
JavaScript
Share:

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

Tags

jQuery

Sam Deering has 15+ years of programming and website development experience. He was a website consultant at Console, ABC News, Flight Centre, Sapient Nitro, and the QLD Government and runs a tech blog with over 1 million views per month. Currently, Sam is the Founder of Crypto News, Australia.

Integromat Tower Ad