4 Replies Latest reply: Nov 22, 2011 6:38 AM by insuractive RSS

    handle single quote with javascript

    Phinehas1234 Community Member

      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.co mpany_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?

        • 1. Re: handle single quote with javascript
          Dan Bracuk Community Member

          urlencodedformat

          • 2. Re: handle single quote with javascript
            insuractive Community Member

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

            • 3. Re: handle single quote with javascript
              cfinnov Community Member

              <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>

              • 4. Re: handle single quote with javascript
                insuractive Community Member

                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.co mpany_name#</a>