Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

string.replace

Status
Not open for further replies.

vlitim

Programmer
Sep 2, 2000
393
GB

I have a text box which contains numbers like:

,541,54,457

and the function below to remove the number if passed to it. The problem is if they pass the number 54 then it will remove the first instance 54(1). How do you get it to look just for that number.



function addProductArea(id)
{

var myString;
var pattern = id;

var newString = myString.replace(pattern,"");
PA.detailsProduct.value=newString ;

}
 
(untested)
function addProductArea(id){
var myString=PA.detailsProduct.value;
var bln_IDFound=false;
var pattern = id;
var newArray = myString.Split(",");
for (var i=0; i<newArray.length; i++)
if (!bln_IDFound && newArray == id){
bln_IDFound=true;//Comment this out to
//remove all instances of 'id'
newArray = &quot;&quot;;
}
var newString = &quot;&quot;;
for (var i=0; i<newArray.length; i++)
if (newArray != &quot;&quot;)//comment out
//this line to include double commas
newString += &quot;,&quot; + newArray;
}
PA.detailsProduct.value=newString;
} codestorm
Fire bad. Tree pretty. - Buffy
select * from population where talent > 'average'
<insert witticism here>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top