0

My app read DOM object's attribute value. I want this value to be swapped with some new texts and put back to attribute. The original value is:

"position: absolute; background-image: url(\"http://dealer.raymarine.com/Views/Public/ShowImage.ashx?partno=M81203&view=thumb\")";

but when it should be updated with JS replace method, but nothing is changed, why?

Updating JS code:

var styleValue = ui.helper[0].getAttribute("style");
styleValue.replace("raymarine", "XXX");
styleValue.replace("ShowImage", "ShowImageSystemCreator");
styleValue.replace("view=thumb", "view=png");
ui.helper[0].setAttribute("style", styleValue);
console.log("draggable after text swap: " + styleValue);
P.K.
  • 1,746
  • 6
  • 25
  • 62
  • 6
    `replace` doesn't update the string, it need to be assigned to the same variable `styleValue = styleValue.replace("raymarine", "XXX");` – Tushar Oct 26 '15 at 15:02
  • Remember: strings are immutable. You can't alter them, you can only produce new ones. – Oriol Oct 26 '15 at 15:05

1 Answers1

3

You're not saving the value anywhere!

styleValue = styleValue.replace("raymarine", "XXX");
styleValue = styleValue.replace("ShowImage", "ShowImageSystemCreator");
styleValue = styleValue.replace("view=thumb", "view=png");

Make it a one-liner if you want by stringing replaces:

styleValue = styleValue.replace("raymarine", "XXX").replace("ShowImage", "ShowImageSystemCreator").replace("view=thumb", "view=png");
ᔕᖺᘎᕊ
  • 2,971
  • 3
  • 23
  • 38