0

On my page, I have a div and a button associated with it. If the button is hit, the div text becomes editable, but the current text in it is saved as a javascript variable. When the button is hit again, the original text is placed back into the div. However, if the div contained something like a & or a ', it gets placed back as something like $#39;. How would I check and solve this in my javascript code?

My idea is to check the string that will be returned for something like a &# since that seems to be the pattern they start with, but I'm not sure how to turn it to its proper symbol after that.

1
  • 1
    Please post the code you are using to save and restore the text. Commented Jul 22, 2013 at 20:20

2 Answers 2

1

Don't save the current text in a javascript variable. That's the hard way. Instead, use show and hide. Push the button once, show the editable version and hide the static version. Push the button again, show the static and hide the input. If you need to mess with forms, then you can also disable as you hide, and enable as you show.

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

2 Comments

Hmm. So, within my div I'd have the same text twice, but hide half of it depending on what's going on?
I generally do it with two divs, one right after the other. It's a bit cleaner that way, because you can just run the show and hide on the wrapping divs. Still, yeah, that's the basic idea. It's really easy in jQuery (which has $.show() and $.hide() functions), but it's not hard in vanilla javascript. Just swap the style between "display:block" and "display:none".
0

encodeURIComponent() and decodeURIComponent() will encode and decode for you. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent

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.