Home > OS >  How to compare the html content of 2 divs with JQuery?
How to compare the html content of 2 divs with JQuery?

Time:11-02

I am comparing the html of 2 different divs and works perfect, but if the content is very long then doesnt work.

if ($('#Div1').html().trim() == $('#Div2').html().trim()) { 
    console.log("Divs are identical");
}
else{
    console.log("Divs are different");
}

https://jsfiddle.net/4r6no12y/

CodePudding user response:

it's a problem with Jquery html() function. You can assign the html content to a js variable and check it

var Div1 = $('#Div1').html().trim();
var Div2 = $('#Div2').html().trim();

if ( Div1 == Div2 ) { 
    console.log("Divs are identical");
}
else{
    console.log("Divs are different");
}

https://jsfiddle.net/qLszd904/

  • Related