I'm new to Javascript, so bear with me. The goal of this code is simply to block the words "crap", "ugly", and "brat" inside of the textarea upon form submit. I want it so that, after the user presses submit, the bad words will star out (**). This is purely a practice lesson I've been assigned, so it doesn't need to have any real use.
The problem with this code is that, once you press submit, all the text in textarea disappears. Therefore, there aren't any words to block anymore.
Here's the code:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>Bad Words Blocker Test</title>
<script type="text/javascript" language="javascript">
var buttonPress = function ()
{
var com = getElementById(comments);
var filterWords = ["crap", "ugly", "brat"];
// "i" is to ignore case and "g" for global
var rgx = new RegExp(filterWords.join(""), "gi");
function WordFilter(str) {
return str.replace(rgx, "****");
}
}
</script>
</head>
<body>
<form name="badwords" method="post" action="">
<textarea name="comments" id="comments" rows="5" cols="50"></textarea>
<br />
<input id="formSub" type="submit" onclick="(buttonPress())" value="Submit!" />
</form>
</body>
</html>