Adidev
Adidev

Reputation: 343

Loading dynamic data using html() breaks the script when the loaded content has special characters

I am getting dynamic data from the database and appending to my html using html() function. Everything works fine but when the dynamic data contains '>' or '<' tag as data the script takes it as a html tag and the data and script breaks.

function showKeywordActivity(e, t, n, r, i) {
    var s = $(e).find("input.keywordFrequencyIdList").val();
    var o = $(e).find("input.keywordId").val();
    var u = $(e).find(".keywordValue").val();
    $(".hashKeyword").text("#" + u);
    $("#tabs-1").html('<div class="innerBox"><div class="innerBoxcontent scroll"><div class="loadingWrap"><div class="loading"></div></div></div><div class="dashboardBottombox"></div></div>');
    $.ajax({type: "POST",url: "url",data: {pageUrlHandle: n,keywordId: o,pattern: u,serviceType: t,startDate: r,endDate: i,keywordType: $("#keywordActivityTypeFilter").val(),keywordType: $("#activityFilter").val()},dataType: "html",async: true,success: function(t) {
            $(e).css("background", "#F8FAFF");
            $("#tabs-1").html(t);
            $(".scroll").each(function() {
                $(this).mCustomScrollbar({theme: "light",advanced: {updateOnBrowserResize: true,updateOnContentResize: true,autoExpandHorizontalScroll: false,autoScrollOnFocus: true}})
            })
        },error: function(e, t) {
        }})
}

How to solve this error. anyone please help me

Upvotes: 0

Views: 185

Answers (2)

Demurgos
Demurgos

Reputation: 1682

Use $("#tabs-1").text(t); instead of .html(); to escape special characters.

Upvotes: 2

Fizer Khan
Fizer Khan

Reputation: 92735

If you want to html content to be placed inside some tag(lets say #target), then

 $('#target').html(content);

If you want to place text content, then

  $('#target').text(content);

Upvotes: 0

Related Questions