3

Possible Duplicate:
javascript replace function not working

Here is my JS code:

var imgTitle = "fizz";
var imgTitle2 = "fizz";
var imgInfo = "buzz";
var imgSrc = "foo";
var liHTML = "<li class='imgThumbLi ui-draggable' title='IMG_TITLE'><img class='image' src='IMG_SRC' title='IMG_TITLE'/><div class='imageInfo'><p class='detailTitle'>IMG_INFO</p></div></li>";

// Search and replace all dummy values.
liHTML.replace("IMG_TITLE", imgTitle);
liHTML.replace("IMG_TITLE2", imgTitle2);
liHTML.replace("IMG_SRC", imgSrc);
liHTML.replace("IMG_INFO", imgInfo);

alert(liHTML);

Getting this for print out:

 <li class='imgThumbLi ui-draggable' title='IMG_TITLE'><img class='image' src='IMG_SRC' title='IMG_TITLE'/><div class='imageInfo'><p class='detailTitle'>IMG_INFO</p></div></li>

(Same as before the string replace calls). In Firebug I'm getting an error stating:

c.replace is not a function

Get this error anytime the code snippet above executes. Why isn't this string replace working?!?! Thanks in advance!

Community
  • 1
  • 1
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
  • 6
    Strings are immutable. And the [MDN documentation](https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace) is clear: *"This method does not change the String object it is called on. It simply returns a new string."* Why did you think `.replace()` would work in-place? – Felix Kling Apr 24 '12 at 12:01
  • 1
    where is the placeholder for `IMG_TITLE2`? – Eliran Malka Apr 24 '12 at 12:01
  • Ah... you are also getting an error. What is `c`? The error does not relate to the code you posted. – Felix Kling Apr 24 '12 at 12:04
  • sometimes javascript doesn't update instantly - you might wanna try - liHTML = liHTML.replace("IMG_TITLE", imgTitle); //and then alert – Jatin Apr 24 '12 at 12:43

5 Answers5

11

try

liHTML = liHTML.replace("IMG_TITLE", imgTitle);
Tejasva Dhyani
  • 1,342
  • 1
  • 10
  • 19
1

You need to set the new value on every line.

liHTML = liHTML.replace("IMG_SRC", imgSrc);
binarious
  • 4,568
  • 26
  • 35
1

You're not doing anything with the replaced values. Try:

var imgTitle = "fizz";
var imgTitle2 = "fizz";
var imgInfo = "buzz";
var imgSrc = "foo";
var liHTML = "<li class='imgThumbLi ui-draggable' title='IMG_TITLE'><img class='image' src='IMG_SRC' title='IMG_TITLE'/><div class='imageInfo'><p class='detailTitle'>IMG_INFO</p></div></li>";

// Search and replace all dummy values.
liHTML = liHTML.replace("IMG_TITLE", imgTitle);
liHTML = liHTML.replace("IMG_TITLE2", imgTitle2);
liHTML = liHTML.replace("IMG_SRC", imgSrc);
liHTML = liHTML.replace("IMG_INFO", imgInfo);

alert(liHTML);
Ben Emmett
  • 450
  • 1
  • 3
  • 9
0

You have to save the results in the variable liHTML:

liHTML = liHTML.replace("IMG_TITLE", imgTitle);
liHTML = liHTML.replace("IMG_TITLE2", imgTitle2);
liHTML = liHTML.replace("IMG_SRC", imgSrc);
liHTML = liHTML.replace("IMG_INFO", imgInfo);

// EDIT Ups a bit to slow :D

Xederas
  • 1
  • 1
0

You have couple of problems there. First of your not setting the value. Replace just generates new value. Second problem is that in your case you should probably use replace-all instead of replace.

Replace replaces only the first instance.

// Scope should have one var. Combine those vars.
var imgTitle = "fizz",
    imgTitle2 = "fizz",
    imgInfo = "buzz",
    imgSrc = "foo",
    liHTML = "<li class='imgThumbLi ui-draggable' title='IMG_TITLE'><img class='image' src='IMG_SRC' title='IMG_TITLE'/><div class='imageInfo'><p class='detailTitle'>IMG_INFO</p></div></li>";

// Search and replace all dummy values.
liHTML = liHTML.replace(new RegExp("IMG_TITLE", 'g'), imgTitle);
liHTML = liHTML.replace(new RegExp("IMG_TITLE2", 'g'), imgTitle2);
liHTML = liHTML.replace(new RegExp("IMG_SRC", 'g'), imgSrc);
liHTML = liHTML.replace(new RegExp("IMG_INFO", 'g'), imgInfo);

alert(liHTML);
Kirstein
  • 893
  • 8
  • 10