JavaScript - Simple Remove Words From Paragraph
Submitted by razormist on Sunday, May 19, 2019 - 00:12.
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.
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!
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.- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
- <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
- </head>
- <body>
- <nav class="navbar navbar-default">
- <div class="container-fluid">
- </div>
- </nav>
- <div class="col-md-6 well">
- <hr style="border-top:1px dotted #ccc;"/>
- <div class="col-md-8">
- <form>
- <div class="form-group">
- <input type="text" id="content"/>
- </div>
- </form>
- </div>
- </div>
- </body>
- </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.- var paragraph = "The question how knowledge should be defined is perhaps the most important and difficult of the three with which we shall deal.";
- document.getElementById('result').innerHTML = paragraph;
- var newParagraph = "";
- function removeWord(){
- var content = document.getElementById('content');
- var word = content.value;
- if(content.value == ""){
- alert("Please enter something first!");
- }else{
- if(newParagraph == ""){
- newParagraph = paragraph.replace(new RegExp(word, 'g'), "");
- }else{
- newParagraph = newParagraph.replace(new RegExp(word, 'g'), "");
- }
- document.getElementById('result').innerHTML = newParagraph;
- content.value = "";
- }
- }
Add new comment
- 164 views