Modify code contains in variable using JQuery. Remove attribute and wrap by div

Viewed 20

I have html in that i have a variable and try to modify it and apply after but after i change it code doesn't change.

So basiclly i have a code with table and i what to remove attribute and wrap by div.

let paste = editor.node.innerHTML;
    //console.log(paste);
    
    console.log($(paste).filter("table"))
    $(paste).filter("table").each(function(i, item){
    
        console.log($(this).attr('width'))
        $(this).removeAttr('width');
        console.log(item)
    })

    editor.node.innerHTML = paste
    console.log(paste)
});

I successfully get that width but after loop i made log and it the same as it was before loop and attribute is still there

Give me an advice please

1 Answers

Your code

$(paste)

creates a jquery object, this then has the.filter/.each applied to - ie to the jquery object. Not to the source string. The original paste variable is unchanged.

To update your variable, you need to reassign the result of your manipulation, ie:

paste = $(paste)
            .filter("table")
            .each(function(i, item) {
                ...
            })
            .html();
Related