473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Javascript Form Input Question

Hi I have a question, it's probably very basic but I'm still learning the Javascript basics.

The following form outputs a string change (from "input" to "text" box ). When the word "test" is inputted, it changes to "good".

Simple question: If there is no matching string, how would I get the form to just display nothing at all?

Thanks!

[HTML]
<html>

<head>

<center>
<script type = "text/javascript">

function replaceChars() {
var temp = document.subform.input.value;

temp = temp.replace(/test/gi, "good");
temp = temp.replace (/hello/gi, "bad");
temp = temp.replace (/today/gi, "tomorrow");

document.subform.text.value = temp;
}

</script>

</head>

<body>


<form name="subform">


<FONT size=+2>
<textarea rows="8" name="input" cols="34" value=""></textarea></FONT>
<br><br>
<input type=button name=action value="Change" onClick="replaceChars(document.subform.input.value );">
<input type="reset" value="Reset" name="B2">

<br><br>

<table border="1" width="30%" id="table1" height="68" bgcolor="#FFFFFF">
<tr>
<td align="center" width="70"><INPUT TYPE="button" NAME="t" VALUE=" t " OnClick="subform.input.value += 't'"></td>
<td align="center" width="70"><INPUT TYPE="button" NAME="e" VALUE=" e " OnClick="subform.input.value += 'e'"></td>
<td align="center" width="70"><INPUT TYPE="button" NAME="s" VALUE=" s " OnClick="subform.input.value += 's'"></td>
<td align="center" width="70"><INPUT TYPE="button" NAME="t" VALUE=" t " OnClick="subform.input.value += 't'"></td> </tr>
</table>


<br><br>

<textarea rows="8" name="text" cols="34" value=""></textarea>

</form>

</center>

</body>

</html>[/HTML]
Aug 21 '07 #1
5 1388
gits
5,390 Expert Mod 4TB
hi ...

you may do something like in the following example:

Expand|Select|Wrap|Line Numbers
  1. var re = /test/gi;
  2.  
  3. temp = re.test(temp) ? temp.replace(re, 'good') : '';
kind regards

ps: please use code tags when posting code
Aug 21 '07 #2
Cool, thanks for responding!

If I have many other words to find and replace? Is there a better way to code it for that purpose?
Aug 21 '07 #3
gits
5,390 Expert Mod 4TB
hi ...

yes ... of course ;) have a look at the following example:

Expand|Select|Wrap|Line Numbers
  1. var word_list = ['foo', 'bar', 'foobar'];
  2.  
  3. for (var i = 0; i < word_list.length; i++) {
  4.     var word = word_list[i];
  5.     var re = new RegExp(word, 'gi');
  6.  
  7.     temp = re.test(temp) ? temp.replace(re, 'good') : '';
  8. }
  9.  
kind regards
Aug 21 '07 #4
Cool, thank you very much!
Aug 21 '07 #5
gits
5,390 Expert Mod 4TB
no problem ... glad to be able to help you ;) post to the forum again whenever you have more questions

kind regards
Aug 21 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the...
6
by: sakms | last post by:
I am attempting to validate radio buttons in Netscape with JavaScript. Everything works excellent in Explorer, but refuses to work in Netscape (all versions). =========== JAVASCRIPT CODE...
2
by: not | last post by:
Hello All, I am trying to develop a relatively simple self-quiz form in javascript, but I'm having no luck getting it to work. What I am looking for is a script that allow the user to select...
4
by: John Boy | last post by:
Hi, Can anyone help. This is really doing my nut in. 3 years ASP exp. and now doing .DOT which is a step in the wrong direction. Basically I am left with the code of a guy who has left. When I...
15
by: Asterbing | last post by:
Already posted in comp.lang.javascript but not found any solution :-( -- Hi all, Don't know where to ask my question because the way to go is included in the possible answer itself by...
4
by: emily224 | last post by:
Hello, I have been trying to understand this source code, which I retreived from my online course test. I would like to know how to find the answer for the question on the test. Im sure the answer...
8
by: danjoplin | last post by:
I've only recently started javascript and I'm a java programmer so I'm sure I've done something obvious that I can't see. Basically this goes with an html document which has a number of questions on...
4
by: Ronald Raygun | last post by:
I have a form on an HTML page with the usual user name, email etc. I want to be able to display a popup window (callout?) when a text input control element is clicked. For example, for the form:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.