jQuery trim method

jQuery trim method

In this blog, let us know how can we remove leading and trailing spaces in any string in jQuery.

jQuery trim is one of the utility methods to remove the leading and trailing whitespaces in the string which is provided as argument.

Let us see the syntax,

$.trim(inputString)

Also we are going to see the jQuery example code for using trim method and output of the program.


<!DOCTYPE html>
<html>
<head>
    <title>Page Title</title>
    <script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
    <script type="text/javascript">
        $(function () {
            var str = "     extra spaces in the string    ";
            var updatedStr = $.trim(str);
            $('#inputStr').text(str);
            $('#resultStr').text(updatedStr);
        });
    </script>

</head>
<body>
    <pre id="inputStr"></pre>
    <pre id="resultStr"></pre>
</body>
</html>
Output:

trim result

This jQuery code to remove the leading and trailing whitespaces of str variable and assigns to updatedStr variable.

Also prints hte difference using pre element to understand the ddifference.




Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^