How do I escape a single quote ( ‘ ) in JavaScript? [duplicate]

UPDATE:
I want to give an updated answer to this question. First, let me state if you’re attempting to accomplish what I have below, I recommend that you manage events by adding event listeners instead. I highly recommend that you utilize jQuery for your project and use their syntax to manage event listeners over using DOM.

QUESTION

Okay, I am basically doing this:

document.getElementById("https://stackoverflow.com/questions/16134910/something").innerHTML = "<img src="https://stackoverflow.com/questions/16134910/something" onmouseover="change(\"ex1\')' />";

I don’t want double quotes (“) where I put the \’. I only want a single quote, so I am trying to not make it put a double when it is used. I am trying to reach this in the final outcome.

<img src="https://stackoverflow.com/questions/16134910/something" onmouseover="change('ex1')" />

Escaping isn’t working for me.

My marked answer works fine, however, the cleaner (and more professional-looking way, IMO) is loganfsmyth’s answer.

5 Answers
5

Leave a Comment