Search code examples
sqlfunctiontagsescapingconcatenation

how to show strings with tags (<>) correctly in the interface


My string inside a tag does not show up because I need to concatenate it, how do I do so?

success: function (response) {
    var tbody = "";
    $.each(response.all_legends, function (key, legend) {
        tbody += '<tr>'+
                    '<td>'+'<AGE>'+'</td>'+
                    '<td>'+legend.text+'</td>'+
                '</tr>';
        });
    $('#legend-list tbody').html(tbody)
}

I tried adding additional + and quotations but the tag still does not show up

CLARIFICATIONS:

legend.text works since it is a regular string. its the <AGE> tag that does not show up. I tried to console.log the tables, it shows up normally in the console but does not appear in the

UPDATE:

Database:

1

Interface:

2

For Clarification, because the first example was a test to fix the tags. I need to mention further that the string has the tags so I cannot just simple encase it in some kind of escape like &lt;AGE&gt

Here is what I tried:

'<td><a href="" onclick="appendMessageText(this)" class="blue-1">'+escape(legend.value)+'</a></td>'+

I can retrieve the database column using a loop with legend.value (1st screenshot). adding the escape turns the tags into its entities (2nd screenshot). How do I fix up the strings with tags?


Solution

  • I think make sense this code. If '<' and '>' chars are at the beginning and at the end, this code help you

    const editText = (text) => `<a><</a><a>${text.replace('<','').replace('>','')}</a><a>></a>`;
     
    success: function (response) {
    var tbody = "";
    $.each(response.all_legends, function (key, legend) {
        tbody += '<tr>'+
                    '<td>'+editText(legend.value)+'</td>'+
                    '<td>'+legend.text+'</td>'+
                '</tr>';
        });
    $('#legend-list tbody').html(tbody)
    

    }