Stonydallas Stonydallas - 1 month ago 7
HTML Question

Beginner alert. I need to get information from a webpage that's submitted by a user

I am making a find and replace algorithm for my website.
The aim is to let the user enter his/her text and it be run through an algorithm that changes certain words. The topics that I am trying to address here are


  1. How to get the text from the user.

  2. How to run it through the algorithm.

  3. How to make the algorithm.



I would like to use HTML or JavaScript, but will go with any thing that gets the job done.

HMR HMR
Answer

Here is a simple program that has things that can be improved but I hope it'll help you get started:

<!DOCTYPE html>
<html>
<head>
<script src="jquery.js"></script>
<script>
function replaceText(){
    // witth is not a spelling error, you cant
    // use with for a variable name
    // create array of replace objects
    var rep =[
        {replace : "cat", witth : "hat"}, 
        {replace : "dark", witth : "light"}
    ], // declare the rest of the variables
    i,
    // get value of the textarea
    text=document.getElementById("txt").value,
    // return text is now the value of the textarea
    returnText=text;
    // for every object in the rep (replace) array
    for(i=0;i<rep.length;i++){
        // replace "replace" with "witth" using
        // rep array
        returnText=returnText.replace(new RegExp(
          rep[i].replace,"igm"),rep[i].witth);
    }
    // set value of the text area
    document.getElementById("txt").value=returnText
}
</script>
</head>
<body >
<textarea id="txt">The dark cat.</textarea>
<input type="button" onclick="replaceText();" value="Replace"/>
</body>
</html>