Search code examples
javascripthtmlescapinghtmldecode

Can I target multiple divs (with the same class) to escape HTML special entities with javascript?


My Webflow search results page is rendering html special entities, which I need to render as html.

My current javascript takes the content of the first search result, decodes it, & places that code into all subsequent search results (divs with the class "html-conversion"). Instead, I need it to individually decode each search result.

// Function that unespaces HTML
function htmlDecode(input){
    var e = document.createElement('div');
    e.innerHTML = input;
    return e.childNodes.length === 0 ? "" : e.childNodes[0].nodeValue;
}

// Store value of html into variable
var code = $(".html-conversion").html();

// Store unescaped value into new variable
var formattedCode = htmlDecode(code);

// Place new value back into the embed
$(".html-conversion").html(formattedCode);

Solution

  • you can use cycle, to replace all - instead of identify one particular

      $( "html-conversion" ).each(function() {
        var code = $(this).html();
        var formattedCode = htmlDecode(code);
        $(this).html(formattedCode);
      });
    

    or if you need identify one particular and you have a way get index of element than try $( "html-conversion:eq( indexhere )" )