I have an element with a rel attribute that contains a JSON string, something like:
rel='{"id":"#id#","name":"#name#"}'
Then, in my javascript code, I use $.parseJSON
to parse this data. This works correctly - besides for cases where name
contains an apostrophe. I've tried using jsStringFormat
, a coldfusion replace
that replaces all single quotes with escaped single quotes, etc, but I can't seem to hit on a correct solution. I know this is probably simple, but how do I get the code to correctly pass values with apostropes/single quotes using json?
This code works, but eliminates the apostrophes which I'd like to preserve:
rel='{"id":"#id#","name":"#replace(name,"'","","all")#"}'
This does not work:
rel='{"id":"#id#","name":"#replace(name,"'","\'","all")#"}'
Nor does:
rel='{"id":"#id#","name":"#replace(name,"'","\\\'","all")#"}'
Or:
rel='{"id":"#id#","name":"#replace(name,"'",""","all")#"}'
Or:
rel='{"id":"#id#","name":"#jsStringFormat(name)#"}'
After lots of playing around, I finally got this to work :)
rel='{"id":"#id#","name":"#replace(name,"'","&##39;","all")#"}'