replacing words in paragraph using javascript

Sometimes it is important to replace some words from a paragraph. This can be done at client end using javascript. In the following example, we want to replace two words “class” and “javascript” from the text written in textarea. The words are replaced with “***” strings.

<!DOCTYPE html>
<html>
<head>
    <title>Untitled document</title>
  <meta charset="utf-8" />

    <script language="javascript" type="text/javascript">
        function myfunction()
        {
            var txt = document.getElementById("txta");

            txt.value = txt.value.replace(/class/gi, '***');
            txt.value = txt.value.replace(/javascript/gi, '***');
        }
    </script>
</head>
<body>
<textarea id="txta">This is web engineering class and we are learning javascript class.</textarea>
    <br />
<input id="btn" name="btn" type="button" 
   value="Click"    onclick="myfunction();" />
</body>
</html>

 

 

Add a Comment

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