Solved

Modifying existing script to add a word before the first word in an input field and add words in the spaces between the other words confused read on...

Posted on 2004-10-13
3
168 Views
Last Modified: 2006-11-17
<script>

function doAndSearch(theForm){
     var sText = theForm.SIMPLE.value.toLowerCase();
     var sText = sText.replace(/<THESAURUS>/gi," ");
     var sText = sText.replace(/\s+/g," ");
     var vWords = sText.split(" ");
     var stringOUT = vWords.join("<THESAURUS>");
     theForm.SIMPLE.value = stringOUT;
               theForm.submit();
              }
</script>
<form NAME="theForm" METHOD="POST" ACTION="/cgi-bin/webcgi.exe">
<INPUT TYPE=TEXT NAME=SIMPLE VALUE='###REPORTED_QUERY###' SIZE=40 style="width:300px">
</form>

I need to modify the above script currently it looks for spaces in the string and then add's a <THESAURUS> between the words and submits the form.

I need to modify this so that

the first word is prefixed by <THESARUS> and other words use the existing logic to add <AND><THESAURUS> so the following

dvd surround sound

will become

<thesaurus>dvd<and><thesaurus>surround<and><thesaurus>sound


thx


0
Comment
Question by:sjsharp80
  • 3
3 Comments
 
LVL 63

Expert Comment

by:Zvonko
ID: 12296503
Does this help:

<script>

var myStr = "dvd surround sound";

var newStr = myStr.split(" ").join("<and><thesaurus>");
if(newStr) newStr = "<thesaurus>"+newStr;


alert(newStr)

</script>

0
 
LVL 63

Expert Comment

by:Zvonko
ID: 12296510
Or one-liner:

<script>

var myStr = "dvd surround sound";

var newStr = "<thesaurus>"+myStr.split(" ").join("<and><thesaurus>");


alert(newStr)

</script>

0
 
LVL 63

Accepted Solution

by:
Zvonko earned 450 total points
ID: 12296532
Or in your form:

<script>

function doAndSearch(theForm){
     var sText = theForm.SIMPLE.value.toLowerCase().replace(/^\s*|\s*$/g,"");
     var sText = sText.replace(/<THESAURUS>/gi," ");
     var sText = sText.replace(/\s+/g," ");
     var vWords = sText.split(" ");
     var stringOUT = vWords.join("<AND><THESAURUS>");
     if(stringOUT>"") stringOUT = "<THESAURUS>"+stringOUT;
     theForm.SIMPLE.value = stringOUT;
     alert(theForm.SIMPLE.value)
}
</script>
<form NAME="theForm" METHOD="POST" ACTION="/cgi-bin/webcgi.exe" onSubmit="doAndSearch(this)">
<INPUT TYPE=TEXT NAME=SIMPLE VALUE='###REPORTED_QUERY###' SIZE=40 style="width:300px">
</form>

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
filter portfolio grid display on page load (wordpress) 7 26
Button and js nou working 3 19
how can i get the string decoded ? 2 28
Search Item in Table 2 19
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question