0

I need to include a JavaScript statement in a visualforce page that includes single quote marks, like this:

formatter: function (cellvalue, options, rowObject) {

   var link = '';

   link = '<a href="#" ' + 
   'onClick="showInfoPopup(''' + rowObject.geocode + '''); return false;"' + 
   '>' + 'info' + '</a>';

    return link;

},

where rowObject is a JavaScript object in the page, not something coming from an Apex Controller.

I've tried using \' and ''', but I can't get my page to compile.

What is the proper way to escape single quote marks in a JavaScript statement in a Visualforce page?

3
  • 1
    I think JSENCODE and JSINHTMLENCODE is what you need
    – javanoob
    Sep 29, 2015 at 18:25
  • You are placing this inside script tags in visual force?
    – manjit5190
    Sep 29, 2015 at 18:30
  • @manjit_singh Yes, I am. Sep 29, 2015 at 19:04

2 Answers 2

0

To be clear, You can use both double quote (") and single quote (') to define a string in javascript, the condition is that the start and end character should be the same i.e. if you start string with double quote (") then you should end it with a double quote only. As you are rightly trying the escape character is backslash (). So, this code should work:-

var link = '<a href="#" onClick= "alert(\'' + rowObject.geocode + '\'); return false;">info</a>';
1
  • I thought I tried this before and it didn't work. But now I think I had other syntax issues with my statement. This is now working. Thanks! Sep 29, 2015 at 19:34
0

Your code should look like this. Hope this helps.

var link = '<a href="#" onClick= showInfoPopup('+rowObject.geocode+'); return false;>info</a>';
2
  • I'm pretty sure I need double-quotes around the Javascript in the onclick event. Also, I need single quotes around the rowObject.geocode value. Sep 29, 2015 at 19:03
  • @MichaelSobczak Have you tried my code. I have tested before posting. Please see the jsfiddle link jsfiddle.net/t5cc2ext. Click on the info link on Result section and you will get the alert.
    – Saroj Bera
    Sep 29, 2015 at 20:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .