0

I've been trying for hours and I've looked a lot of samples on StackOverflow, but I can't fix my simple script.

I grabbed DOM with jQuery

var color = $('#bscontainer').html();

and now the content of color is:

<img src="resources/P/Blue_BG_2Col.png" id="bg">
<img src="resources/P/Blue_Content_2Col.png" id="content">
<img src="resources/P/Blue_Title_ExchangeRate.png" id="title">
<img src="resources/P/Blue_SubTitle_2Col.png" id="subtitle">
<img src="resources/P/Blue_Disclaimer_Disclaimer.png" id="disclaimer">

My idea is to change all the Blue to Green, and I already try this:

curColor="Blue";
newColor="Green";
t=color.replace(curColor,newColor);

It simply doesn't works. Any ideas?

5
  • 1
    @Blazemonger A bit harsh :) Commented Feb 28, 2013 at 21:50
  • It works for me. You must be doing something wrong in code that you haven't provided, or you are expecting more from the code than the methods being used are meant to provide. Commented Feb 28, 2013 at 21:51
  • I don't understand what your trying to do here..explain better Commented Feb 28, 2013 at 21:51
  • 1
    when you say it doesn't work, what do you mean? You've shown no code where you actually write your changes back to the file, so if you're hoping to load new images, this won't do anything. This will just give you some HTML with scripts pointing to the green resources. You haven't done anything with it yet. Commented Feb 28, 2013 at 21:53
  • i tried to alert(t)... it still display the 'Blue' content Commented Feb 28, 2013 at 22:00

3 Answers 3

1

To answer your question directly, replace in javascript when it takes a string parameter as the needle, only replaces the first instance. To replace all instances within a string, use a global regular expression:

str.replace(/Blue/g, 'Green');

You really ought to just modify the src attributes of the img tags though. That is the more proper way to attack this. Changing the html like you are trying to will lose any events or data bound to the DOM elements.

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

2 Comments

the problem is Blue and Green actually a variable... for example, var curColor="Blue"; var newColor="Green"; i can't make the regex works, when i use variables
@user2121401 You can if you construct a regexp object.
0

So basically you want to find each image in your color element and replace Blue with Green in the src attribute -- This is one way you can do it:

var color = $('#bscontainer');
color.find('img').each(function(){

    curColor="Blue";
    newColor="Green";
    this.src = this.src.replace(curColor,newColor);

});

No need for the .html() part of your code...

Comments

0

Use a callback function to actually update the HTML, and use a regular expression (object) to replace all instances at once:

$('#bscontainer').html(function (i, str) {
    curColor = new RegExp("Blue","g"); // global replace
    newColor = "Green";
    return str.replace(curColor, newColor);
});

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.