# Solved: Apostrophe (single quote) causes onclick to fail



## andynic (May 25, 2007)

Hi
Can anyone shed some light on this problem?

Given the following tag:

Everything works perfectly until the user puts an apostrophe (single quote) in the "recensie" text (which is a field..
I convert it to ' ( and have also tried &apos; )
Still it causes the onclick to fail (go dead as it were) as if a single quote were embedded in the string.
All other special characters such as < ( converted to < ) etc. work just fine.

Would appreciate any insight/workaround anyone can give me.

Thanks for your help.
Andynic


----------



## andynic (May 25, 2007)

Don't know why the above does not work, but in the meantime I have found a work-around:

I moved the functionality of javascript code to php code, i.e. the php code assigns the values to the hidden fields rather than the javascript commands such as "document.getElementById('userName').value = 'Andy Nicastro';" and the like.

Still don't understand why &apos; or &#039; should be interpreted as a single quote in the storage of the string (as opposed to the display of the string).

I'll keep the post "unsolved" for a few days to see if someone can explain what is going on.
Andynic.


----------



## allnodcoms (Jun 30, 2007)

Hi Andy,

Have you tried bundling all that up into a function and calling it in your onClick? I'd use 'escape();' and 'unescape();' to html encode the parameters as well, but it may work without.

Javascript is a funny old thing (not that funny if you have to work with it of course) and it sounds, as you point out, that the single quote is terminating your variable when the onClick string is parsed. That wouldn't happen when calling a function, as the value would be 'passed' and not 'parsed', if you get my drift...

Why does it happen? It's the internet... who knows! 

Danny


----------



## andynic (May 25, 2007)

Thanks Danny.
I hadn't thought of moving the code into a javascript function. Will give it a try.
Andynic


----------



## Ent (Apr 11, 2009)

Once it's all bundled up in a function of its own, you may still need to take out that apostrophe. You can use the backslash character to escape it.
'andynic xxx\'x recensie';


----------

