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
167 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…

832 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