Remove spaces in a string using jquery

In this programming tutorial you will learn how to remove spaces in a string using jquery. Its quite easy in jquery. I will us the built-in replace() method to do it. Let's have a look over the following code snippet.

Remove spaces in a string using jquery
<html xmlns="http://www.w3.org/1999/xhtml">

<head>

    <title>Remove all spaces in a string using jquery</title>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script></script>

    <script type="text/javascript">

        $(function () {

            var spanText = $("#spanOne").html();

            $("#spanTwo").html(spanText.replace(/\s/g, ""));

        });

    </script>

</head>

<body>
   <p><strong>Before</strong></p>

   <p><span id="spanOne">For more bundle of jquery tutorials, stay tuned with nice-tutorials.blogspot.com</span></p>

    <p><strong>After</strong></p>

    <p><span id="spanTwo"></span></p>

</body>

</html>

Output
Remove spaces in a string using jquery
So that's it. Using regular expression within replace method we have successfully remove spaces in a string using jquery.



I love your feedback.

0 comments: