0

This seems like it should be simple, but it's proving to be a pain. How do you set the src of an image to a name containing spaces? I tried this way:

var newSrc = "images/" + sname[field].split(":")[selected] + ".png";
document.getElementById("image" + field).src = newSrc;

as well as putting the calculation directly into the space occupied by the newSrc variable. However, the image never displays despite it being displayed on another element, and retrieval via

document.getElementById("image" + field).src

shows it's in fact been set as

http://localhost/SkillPlanner/images/Branch%202.png

Technically, I guess there's two questions:

1) Why is it fully qualifying it when all I need is a relative link?

2) How can I keep the spaces intact as they are part of the filename?

Todd Davis
  • 11
  • 1
  • 3
  • Found a solution by doing the dumbest thing I could think of, which was to wrap the entire string I was adding in "unescape()". Still don't know why it worked, but it did :P Have to wait 5 hours to answer the question so I can add the answer and mark it solved :P – Todd Davis Jul 04 '13 at 20:39

1 Answers1

1

As promissed (if a bit delayed), the solution was to wrap the string in "unescape", which forces thing like spaces to NOT be interpreted when used.

var newSrc = unescape("images/" + sname[field].split(":")[selected] + ".png");
Todd Davis
  • 11
  • 1
  • 3