jQuery trim example

To trim string value using jquery simple use $.trim()

Below example will show you how to trim a string.

var str = " Hello "; str = $.trim(str);

Below example will trim the textbox value when click on Trim button.

Input :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Login Page</title> <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script> <script type="text/javascript"> function trimtext() { var str = $("#txtInput").val(); str = $.trim(str); $("#txtInput").val(str); } </script> </head> <body> <div> Input : <input type="text" id="txtInput" value=" Hello " /> <input type="button" value="Trim" onclick="trimtext();" /> </div> </body> </html>