Home > Blockchain >  replaceWith includes a string and not an html element
replaceWith includes a string and not an html element

Time:10-26

I try to replace an html element by a new html element which has been computed and returned by twig. But the replaceWith function understands it as a string and not an html element

function replaceItem(item, html) {
    item?.replaceWith(html);
}

Before replaceItem enter image description here After replaceItem enter image description here

enter image description here

Sometime partialy worked enter image description here

Have try this :

function replaceItem(item, html) {
    newItem = create.element(html);
    item?.replaceWith(newItem);
}

function replaceItem(item, html) {
    item?.replaceWith(html).html();
}

CodePudding user response:

Since item is a DOM element, you're calling the DOM method, not the jQuery method. The DOM version doesn't interpret HTML, it treats it as literal text.

So use the jQuery method:

function replaceItem(item, html) {
    if (item) {
        $(item).replaceWith(html);
    }
}
  • Related