Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


how can I pass a string with apostrophe

Posted on 2015-01-30
Medium Priority
Last Modified: 2015-01-30

I need your help please, I have an onclick event that I am passing a variable.

the value of that variable has apostrophe. How can I pass my value with apostrophe in it?

here's what I have

the value for "cn" is = 'O'brianes'

onClick="return SendValue('#UrlVal#');">

so this is how it looks

onClick="return SendValue('O'brianes');">

How can I fix this issue? 

Please help thank you 

Open in new window

Question by:lulu50
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 3
LVL 143

Accepted Solution

Guy Hengel [angelIII / a3] earned 2000 total points
ID: 40579477
you have to escape it:

onClick="return SendValue('O\'brianes');">

using escape or encoreURI

onClick="return SendValue(escape('#UrlVal#') );">

Author Comment

ID: 40579485

I changed the code to this: 
onClick="return SendValue(escape('')">

My output now is this: 
return SendValue(escape('O'Brien, Smith'));

Open in new window

LVL 43

Expert Comment

ID: 40579509
I suggest you use double quotes:

onClick="return SendValue(\"#UrlVal#\");">
The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40579520
looks like I confused the "html/url" part ...

you may want to try eval...
onClick="return SendValue(eval('') )">

anyhow, where does the value come from?
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40579523
using double quotes will not help if the value contains double quotes...

Author Comment

ID: 40579528
I tried double quotes and that did not work

the value coming from the database

Author Comment

ID: 40579536
How can I in Javascript
make this value :
onClick="return SendValue('' )"

turn to this value:


if I replace '  with \'  then it will work (How can I do the replace in javascript)

onClick="return SendValue(''/g, "")' )"

something like the above syntax

Author Comment

ID: 40579547
if I do this then it works

onClick="return SendValue('O\'brianes')

so I need to replace '    with    \'   in order to work with my passed variable

Author Comment

ID: 40579556

but I need to add the \  to get to this result   O\'brianes  I don't know where to put the \

Author Comment

ID: 40579558
oh I got it

Author Comment

ID: 40579560
Thank you Guy for telling that I have to put \  when I did it works fine

onClick="return SendValue('#REreplace(, "'", "\'", "all")#')

Author Closing Comment

ID: 40579563
Thank you all for your help

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently while working on a project I got a very annoying cfdocument has no body error message. I had never seen this error before. So I checked the code. The code was pretty simple; it was Just showing me the cfdocumnt tag and inside that tag a …
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question