How to remove all whitespace from string in JQuery?

By Hardik Savani | May 24, 2016 | | 2778 Viewer | Category : Javascript HTML jQuery


Share this post:

Sometimes we require to remove white space from given string. So at that time you can delete spaces from string 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. you can do it from 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>