getting rid of spaces

I have some code that I am trying to use for a link.  However, the spaces are messing up that link.  How can I get rid of them?

I tried this but my code just errors out.  

+ "<br> <a href=" + str_replace(" ", "%20", graphic.attributes.SAPALINK) + ">SAPA Report</a>"

Thanks I'm hoping for a simple solution here.  FWIW - This is in a mapping application and going inside an info box but it is using javascript to do so.
Who is Participating?
leakim971Connect With a Mentor PluritechnicianCommented:
add simple quote around for the href and use encodeURIComponent :

+ "<br> <a href='" + encodeURIComponent(graphic.attributes.SAPALINK) + "'>SAPA Report</a>"
Lee SavidgeCommented:
Assuming the field is called graphic.attributes.SAPALINK

<a href=" + graphic.attributes.SAPALINK.str_replace(" ", "%20") + ">SAPA Report</a>

Open in new window

Lee SavidgeCommented:
Or leakim971's option! Always forget about encodeURIComponent!

Much better than replacing characters.
flfmmqpAuthor Commented:
You answer helped me quickly get to this.  Which seems to be working.  Whohoo  Thanks

+ "<br> <a href='" + graphic.attributes.SAPALINK + "'>SAPA Report</a>"
Yes, the encodeURIComponen as it's your link not a parameter for your link
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.