Skip to main content

Remove Word From A Paragraph Using JavaScript


Body

In this article we will create a Remove Word from a Paragraph tutorial using JavaScript. This code will dynamically remove a word from a paragraph when user enter a word and submit it. The code use onClick() event to call a specific function that can remove a word from a certain position within the paragraph by the use of dot notation replace() with RegExp pattern for possible value. Apply this script to your code to manipulate the paragraph easier. You’re free to modify it to suit your needs.

In this script, we show you how useful JavaScript in this scenario. This can be done also using PHP, but JavaScript is more suitable in this case.

Getting Started:

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 this 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. <body>
  8. <nav class="navbar navbar-default">
  9. <div class="container-fluid">
  10. <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11. </div>
  12. </nav>
  13. <div class="col-md-3"></div>
  14. <div class="col-md-6 well">
  15. <h3 class="text-primary">Remove Word From A Paragraph Using JavaScript</h3>
  16. <hr style="border-top:1px dotted #ccc;"/>
  17. <div class="col-md-4">
  18. <form>
  19. <div class="form-group">
  20. <label>Enter a word</label>
  21. <input type="text" id="content"/>
  22. </div>
  23. <center><button type="button" id="remove" class="btn btn-primary">Remove</button></center>
  24. </form>
  25. </div>
  26. <div class="col-md-8">
  27. <p id="result"></p>
  28. </div>
  29. </div>
  30. </body>
  31. <script src="js/script.js"></script>
  32. </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 as shown below inside the text editor and save it as script.js inside the js directory.

  1. var paragraph = "Shattered soul, tattered mind. The way back is what I hope to find. Broken dreams lost without a trace. Lately I've been feeling a little out of place.";
  2.  
  3. document.getElementById('result').innerHTML = paragraph;
  4.  
  5. document.getElementById('remove').onclick = removeWord;
  6.  
  7. var newPa = "";
  8.  
  9. function removeWord(){
  10. var content = document.getElementById('content');
  11. var target = content.value;
  12.  
  13. if(content.value == ""){
  14. alert("Please enter something first!");
  15. }else{
  16. if(newPa == ""){
  17. newPa = paragraph.replace(new RegExp(target, 'g'), "");
  18. }else{
  19. newPa = newPa.replace(new RegExp(target, 'g'), "");
  20. }
  21.  
  22. document.getElementById('result').innerHTML = newPa;
  23.  
  24. content.value = "";
  25. }
  26. }

There you have it we successfully created a Remove Word From A 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!

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.