Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replace html tag with new tag content

The html tag for the page doesn’t have lang=en attribute, I’m trying to add it to html tag, below is the code I did it but it replaces the whole html content instead of just adding lang=en to html.

window.addEventListener('load', function () {
    alert("It's loaded!")
    const cond = document.getElementsByTagName('html')[0]|| false;
    console.log(cond)
     if (cond) {
          $('html').each(function() {
              $(this).replaceWith($('<html lang="en">'));
          });
    }});

Also I tried below code but it doesn’t work too, basically it gets html content and it appends with new html tag and content.

const htmlContent = $( "html" ).html();
    if (cond) {
        $('html').replaceWith('<html lang="en">' + htmlContent +  '</html>');
    }

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This is not the optimal way of doing that because it could cause events to be detached or other performance issues. It would be safer to do:

$('html').attr('lang', 'en')`

This way you are only adding an attribute, nothing else.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading