jhull412
jhull412

Reputation: 60

Escaping apostrophe within JavaScript being injected by Ruby?

I have the following line of code:

page.execute_script(%Q($("div.fu-symptom-question:contains('Could your symptoms be associated with swimming?') label:contains('I have been swimming, but I don't think they are related') input").click()))

The apostrophe in the word "don't" is giving me trouble. I've tried all sorts of ways to escape it, but I can't seem to get it right.

I'm a Ruby noob and don't really have that much JavaScript experience to boot, so I'm kind of out of my element here.

Upvotes: 0

Views: 95

Answers (1)

user229044
user229044

Reputation: 239311

Use a backslash...

page.execute_script(%Q($("div.fu-symptom-question:contains('Could your symptoms be associated with swimming?') label:contains('I have been swimming, but I don\\'t think they are related') input").click()))

You may have been using \', which simply escapes the quote in Ruby. You need to insert a literal backslash, \\, so that the quote is escaped down the wire in JavaScript.

Upvotes: 2

Related Questions