Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

handle single quote with javascript

Guest
Nov 21, 2011 Nov 21, 2011

Hi,

I want to add a hyperlink in every company name that it will show its name in a pop-up dialog. Therefore, I try to do it as the following:

<a href="##" onclick="javascript:select('#replace(getList.company_name#')">#getList.company_name#</a>

However, since some company names have single quote " ' ", it encounters error when handling these company name. Therefore, I try to change the program as the following:

<a href="##" onclick="javascript:select('#replace(getList.company_name,''','\'','ALL')#')">#getList.company_name#</a>

But, it does not work and shows me error "The value ', cannot be converted to a number."

Can anyone help me to solve this problem?

2.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advocate , Nov 22, 2011 Nov 22, 2011

Let me rephrase my answer since I realized that might not have made sense - there is a typo in your syntax in how you are trying to reference the apostrophes.  When defining your replace statement if you use the same character as the text qualifier in the function (in this case an apostrophe), you need to escape the character you are searching for in CF.  The text to replace cannot be three apostrophes:

'''

Your choices are using a different qualifier (quotation marks - will work fine here since t

...
Translate
LEGEND ,
Nov 21, 2011 Nov 21, 2011

urlencodedformat

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 21, 2011 Nov 21, 2011

You can also escape quotation marks and apostrophes in JavaScript using the backslash: \'

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Nov 22, 2011 Nov 22, 2011
LATEST

Let me rephrase my answer since I realized that might not have made sense - there is a typo in your syntax in how you are trying to reference the apostrophes.  When defining your replace statement if you use the same character as the text qualifier in the function (in this case an apostrophe), you need to escape the character you are searching for in CF.  The text to replace cannot be three apostrophes:

'''

Your choices are using a different qualifier (quotation marks - will work fine here since there is no conflict between the server side code and client side code) or escaping the qualifier (apostrophe) in CF by doubling it.

Does this work:

<a href="##" onclick="javascript:select('#replace(getList.company_name,"'","\'","ALL")#')">#getList.company_name#</a>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Nov 22, 2011 Nov 22, 2011

<cfset CompName = "D'zousa and CO">
<cfoutput>
<input type="hidden" name="MyCompany" id="MyCompany" value="#CompName#">
<a href="##" onclick="javascript:mypopup()">#(CompName)#</a>
</cfoutput>
<script language="javascript">
function mypopup()
{

var comp = '';
comp = document.getElementById('MyCompany').value;

alert(comp);

}
</script>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources