Loading ...

How to replace all the blank spaces in Javascript

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  client side web development   » How to replace all the blank spaces in Javascript

How to replace all the blank spaces in Javascript

Posts under the topic: How to replace all the blank spaces in Javascript

Posted: 5/23/2010

Lurker 235  points  Lurker
  • Joined on: 10/16/2009
  • Posts: 47

Hi experts,

I have a string in jabvascript . There is a requirement to replace all the blank spaces with a particular character . How can I do so ?


tags javascript

Posted: 5/26/2010

Guru 16813  points  Guru
  • Joined on: 4/19/2009
  • Posts: 490
  Answered

This will serve you : val.replace(/\s/g, "x") where x is your character. Below is the sample:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
    <asp:Button ID="Button1" runat="server" Text="Test" OnClientClick="return test()" />
    <br />
    </form>
    <script type="text/javascript">

        function test() {
            var txtBox1 = document.getElementById('<%= TextBox1.ClientID %>');
            var val = txtBox1.value;
            var replacedValue = val.replace(/\s/g, "X");
            alert(replacedValue);
            return false;
        }
    
    </script>
</body>
</html>



tags javascript
Page 1 of 1 (2 items)