How To Remove All Spaces From String in jQuery

Reading Time: 3 minutes

Inside this article we will see that how can we remove white spaces from a string using jquery. This tutorial will give you the idea about remove all spaces from string in jquery.

Article will be super easy to understand and very easy to implement in your code.

For example –

String with space as – Online Web Tutor

String with no space – OnlineWebTutor

We will use jquery concept to remove all spaces from a sting.

Learn More –

Let’s get started.

Create an Application

Create a folder with name remove-space. Create a file index.html into it.

Open index.html and write this complete code into it.


    <title>How to remove all spaces from string in JQuery?</title>
    <link rel="stylesheet" href="">
    <script src=""></script>

    <div class="container" style="margin-top: 30px;">
        <div class="row">
            <div class="col-md-12 offset-md-3 mt-5">
                <h4>How to remove all spaces from string in JQuery?</h4>
                <div class="panel panel-primary">
                    <div class="panel-heading">How to remove all spaces from string in JQuery?</div>
                    <div class="panel-body">
                        <form class="form-horizontal" action="#">
                            <div class="form-group">
                                <label class="control-label col-sm-2" for="file">String:</label>
                                <div class="col-sm-10">
                                    <input type="text" class="form-control" id="string" name="string" placeholder="Enter string...">
                            <div class="form-group">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <button type="button" class="btn btn-primary">Remove space</button>
                            <div class="form-group">
                                <div class="col-sm-offset-2 col-sm-10">
                                    <h3 id="remove-space-string"></h3>
    <script type="text/javascript">
            var myText = $("#string").val();
            var remove_space = myText.replace(/ /g,'');


Application Testing

Open browser and type this –

URL – http://localhost/remove-space/index.html

The above method will remove all spaces from string.

If in case if you have string like this –

var string = "      my sample string value   ";

Now, to remove white spaces from string front and end we will use jquery trim() method.


We hope this article helped you to learn Create Profile Image of First Letter of First & Last Name using jquery Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more