jQuery Remove All Spaces From String

Sometimes, you may need to delete all empty spaces from string using jquery javascript. So, this example tutorial will show you an easy way to remove all black space using jquery replace function.

Remove all spaces from string in jQuery; In this tutorial, you will learn how to remove all spaces from string using jQuery.

How To Remove Spaces From String in jQuery

The jQuery replace() method remove all spaces from string.

Here is an example of how to remove all spaces from string in jQuery; as shown below:

<html lang="en">
<head>
    <title>How to remove all spaces from string in JQuery? - Tutsmake.com</title>
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <style>
        body{
            background: rgb(216, 232, 215);
        }
        .container{
            background: rgb(247, 228, 220);
            margin-top: 14%;
            height: 300px;
            width: 51%;
            border: 3px solid red !important;
        }
        .container h4{
            margin-bottom: 42px;
            font-size: 25px;
        }
    </style>
</head>
<body>
    <div class="container text-center border">
        <h4 class="mt-5 font-weight-bold">How to remove all spaces from string in JQuery? - tutsmake.com</h4>
        <textarea class="content-text" cols="55" rows="2">
            tutsmake.com have a collection of Example and Demo of IT.
        </textarea>
        <button class="btn btn-primary mx-auto d-block mt-4 ">Remove White Space</button>
    </div>
    <script type="text/javascript">
        $("button").click(function(){
            myText = $(".content-text").val();
            var remove_space = myText.replace(/ /g,'');
            alert(remove_space);
        });
    </script>
</body>
</html>

Note that, when you click on button, the following jquery code will remove all spaces from string:

    <script type="text/javascript">
        $("button").click(function(){
            myText = $(".content-text").val();
            var remove_space = myText.replace(/ /g,'');
            alert(remove_space);
        });
    </script>

In the above example, use jquery replace() function to remove all spaces from string.

Recommended jQuery Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *