0

I have a jQuery datatable and I'm trying to use a popover to show some comments, but comments containing a single quote is preventing the popover from showing the whole comment.

Within the datatable cell, the entire comment is being shown. Not sure why the popover is failing to show the whole comment.

Here is the code for the datatable (simplified as much as possible):

 $.ajax({
   url: 'process/getTargetSheet.php',
   type: 'POST',
   data: '',
   dataType: 'html',
   success: function(data, textStatus, jqXHR){
     var jsonObject = JSON.parse(data); 
     var table = $('#example1').DataTable({
       "dom": "Rlfrtip",
       "data": jsonObject,
       "columns": [
         {
           "data": "tsc_tgcomment",
           "fnCreatedCell": function (nTd, sData, oData, iRow, iCol)
           {
             var tccomment = oData.tsc_tgcomment;

             $(nTd).html("<a tabindex='0' class='tcDisplay' 
                           data-toggle='popover' data-trigger='focus' 
                           data-content='"+tccomment+"' id='tcDisplay' 
                           data-toggle='modal'>"+tccomment.substring(0, 50)+"...</span>");

             $(function () {
               $('[data-toggle="popover"]').popover();
             });
           },
           // some more columns
         }
       ]
     });
   },
   error: // nothing important here
 });

So in the $(nTd) portion above, you'll see where I added the tccomment into the popover. But strings that have quotes fail to show the whole comment.

I tried to use "escape" in this manner:

data-content='"+escape(tccomment)+"'

But the comment prints out beyond the popover and includes a bunch of percent signs.

How can I fix this to show the whole comment in the popover?

halfer
  • 19,824
  • 17
  • 99
  • 186
John Beasley
  • 2,577
  • 9
  • 43
  • 89
  • isn't your goal to [escape quote in html attribute](https://stackoverflow.com/questions/7753448/how-do-i-escape-quotes-in-html-attribute-values) ? – grodzi Dec 11 '19 at 18:22

2 Answers2

1

You can use .replace this like

var a = "'somehthing with single quote'"

var b = a.replace(/'/g,'"');

Then they'll all be double quotes. If you want a different character or no quote at all, you can modify the end of the replace function.

Sloan Tash
  • 36
  • 4
0

This is how I got it to work, although single quotes are now double quotes:

data-content='"+JSON.stringify(tccomment).replace(/&/, "&amp;").replace(/'/g, "&quot;")+"'
John Beasley
  • 2,577
  • 9
  • 43
  • 89