JavaScript - Simple Remove Words From Paragraph

In this tutorial we will create a Simple Remove Words From Paragraph using JavaScript. This code will code will specifically remove a word when the user input a word and click the button. The code use onclick() function to initiate a certain method by finding a word that matches the inputted value in order to remove the word by using replace method and passing a parameter RegExp. Feel free to modify and apply it in your system, this is a user-friendly kind of program We will be using JavaScript as a server-side scripting language because It gives a greater control of your web page and extend its capability in a modern way approach. It is written in HTML or as an external sourcing to add some necessary features in your website.

Getting started:

First you have to download bootstrap framework, this is the link for the bootstrap that I used for the layout design https://getbootstrap.com/.

The Main Interface

This code contains the interface of the application. To create this just write these block of code inside the text editor and save this as index.html.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7.         <nav class="navbar navbar-default">
  8.                 <div class="container-fluid">
  9.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  10.                 </div>
  11.         </nav>
  12.         <div class="col-md-3"></div>
  13.         <div class="col-md-6 well">
  14.                 <h3 class="text-primary">JavaScript - Simple Remove Words From Paragraph</h3>
  15.                 <hr style="border-top:1px dotted #ccc;"/>
  16.                 <div class="col-md-2"></div>
  17.                 <div class="col-md-8">
  18.                         <form>
  19.                                 <div class="form-group">
  20.                                         <label>Remove Word</label>
  21.                                         <input type="text" id="content"/>
  22.                                         <button type="button" onclick="removeWord();" class="btn btn-primary">Remove</button>
  23.                                 </div>
  24.                         </form>
  25.                         <p id="result"></p>
  26.                 </div>
  27.         </div>
  28. </body>
  29. <script src="js/script.js"></script>
  30. </html>

Creating the Script

This code contains the script of the application. This code will remove a word when the button is clicked. To do this just copy and write these block of codes inside the text editor, then save it as script.js inside the js folder.
  1. var paragraph = "The question how knowledge should be defined is perhaps the most important and difficult of the three with which we shall deal.";
  2.        
  3. document.getElementById('result').innerHTML = paragraph;
  4.  
  5.  
  6. var newParagraph = "";
  7.  
  8.  
  9. function removeWord(){
  10.         var content = document.getElementById('content');
  11.         var word = content.value;
  12.  
  13.         if(content.value == ""){
  14.                 alert("Please enter something first!");
  15.         }else{
  16.                 if(newParagraph == ""){
  17.                         newParagraph = paragraph.replace(new RegExp(word, 'g'), "");
  18.                 }else{
  19.                         newParagraph = newParagraph.replace(new RegExp(word, 'g'), "");
  20.                 }
  21.                
  22.                 document.getElementById('result').innerHTML = newParagraph;
  23.                
  24.                 content.value = "";
  25.         }
  26. }
There you have it we successfully created a Simple Remove Words From Paragraph using JavaScript. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment