In this article I will explain with an example, how to remove White Space from beginning (start) and end of string using jQuery.
When the Remove Button is clicked, a jQuery event handler is called and the whitespace is removed from beginning (start) and end of the TextBox value using jQuery.
HTML Markup
The following HTML Markup consists of an HTML TextBox and a Button.
<input type="text" id="txtName" value=" MUDASSAR KHAN " />
<input type="button" id="btnRemove" value="Remove Whitespace" />
jQuery Code
Inside the document.ready event handler, the Remove Button has been assigned with a Click event handler.
When the Remove Button is clicked, first the TextBox is referenced and then the whitespace is removed from beginning (start) and end of the string using the jQuery trim function.
Finally, the updated string value is re-assigned to the TextBox.
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$("#btnRemove").click(function () {
var txtName = $("#txtName");
var name = $.trim(txtName.val());
txtName.val(name);
});
});
</script>
Screenshot
Browser Compatibility
The above code has been tested in the following browsers.
* All browser logos displayed above are property of their respective owners.
Demo
Downloads