How to remove all whitespace from string in JQuery?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this post i will show you How to remove all whitespace from string in JQuery? Sometimes we must need to remove the white space from the given string. So at that times we can delete the spaces from string by using replace() of core php. replace() will help to replace your blank space or empty space or extra space or trailing space from string that way we can remove.

We can do this from the bellow example.

Example:

<html lang="en">
<head>
    <title>Jquery - Remove white space from string</title>  
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
</head>
<body>


<div class="mytext">
  This   is  my site.
</div>


<script type="text/javascript">
      var myText = $(".mytext").text();
      var newMyText = myText.replace(/ /g,'');
      alert(newMyText);
</script>


</body>
</html>

Read Also : Laravel 8 Mobile Number Verification Tutorial

Thanks for read. I hope it help you. For more you can follow us on facebook

About code chef

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by code chef →