jQuery Page Widget How to - Join text by dash








Question

We would like to know how to join text by dash.

Answer


<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){<!--from w w  w  .  j a  v  a  2s  .c om-->
            var newText = $("p").text().split(" ").join("-");
            
            $("p").html(newText);

        });
    </script>
    <style>
      .y { background:yellow; }
  </style>

  </head>
  <body>
    <body>
       <p>a ab abc</p>
  </body>
</html>

The code above is rendered as follows: