0

I'm ok at PHP, but no nothing about Javascript, so I have no idea how to proceed here. I'm trying to replace spaces with a "+" in line 3. Anybody know why it's not working? Thanks!

var tn_dv_suggestions = new Array();
for(var tn_counter=0; tn_counter < tn_top_performers.length; tn_counter++)
tn_top_performers[tn_counter]=tn_top_performers[tn_counter].replace(" ","+");
tn_dv_suggestions.push("<a style='font-family: Verdana, Arial; font-size: 14px;' target='_blank' href='http://www.<?=$siterow['Domain']?>/Buy-"+escape(tn_top_performers[tn_counter]) +"-<?=urlencode($siterow['CitySearchName'])?>-Tickets' >"+tn_top_performers[tn_counter] +"</a><br />");
document.getElementById('tn_dv_suggestions089hZ').innerHTML=tn_dv_suggestions.join('');
1
  • Can you give us some example input (values in tn_top_performers before the loop) and output (values in tn_top_performers after the loop)? Commented Mar 28, 2011 at 17:01

4 Answers 4

3

Here is a solution using array.map:

var replaceInArray = function(str){
  return str.replace(/\s+/g, "+")
}

var arr = ["Summer is Great", "Winter is terrible"]

arr.map(replaceInArray);
// returns => ["Summer+is+Great", "Winter+is+terrible"]

Your problem was that you were only replacing the first instance of " ". To fix this, use the global flag, by using g with your regex.

Sign up to request clarification or add additional context in comments.

Comments

1

you probably replace only first space found. to replace all of them, you'll need global flag. try .replace(/\ /g, "+");

3 Comments

Thanks for your response. I just tried that, but it only displays "undefined"
well, the replace itself is fine then. your problem must be somewhere else - possibly tn_top_performers[tn_counter] is undefined?
I'm sorry, I'm pretty green at javascript. When I take out the replace line, the results are displayed fine (but still of course have spaces). So that would mean they're defined, right?
0

Your use of String.replace() is fine. The problem is that you are missing curly brackets surrounding all of the statements you want in the loop.

Fixed code:

var tn_dv_suggestions = new Array();
for (var tn_counter=0; tn_counter < tn_top_performers.length; tn_counter++) {
  tn_top_performers[tn_counter]=tn_top_performers[tn_counter].replace(" ","+");
  tn_dv_suggestions.push("<a style='font-family: Verdana, Arial; font-size: 14px;' target='_blank' href='http://www.<?=$siterow['Domain']?>/Buy-"+escape(tn_top_performers[tn_counter]) +"-<?=urlencode($siterow['CitySearchName'])?>-Tickets' >"+tn_top_performers[tn_counter] +"</a><br />");
}
document.getElementById('tn_dv_suggestions089hZ').innerHTML=tn_dv_suggestions.join('');

1 Comment

That did it! Thanks so much for noticing that!
0

Tested on FF3 and Chrome.

tn_top_performers[tn_counter]=tn_top_performers[tn_counter].replace(/ /g,"+");

Edit: Don't forget the " "(space) between the forward slashes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.