Categories
JavaScript Answers

How to read the html element lang attribute value with JavaScript?

Spread the love

Sometimes, we want to read the html element lang attribute value with JavaScript.

In this article, we’ll look at how to read the html element lang attribute value with JavaScript.

How to read the html element lang attribute value with JavaScript?

To read the html element lang attribute value with JavaScript, we can use the getElementsByTagName and getAttribute methods.

For instance, if we have:

<html lang='en'>

</html>

then we write:

const [html] = document.getElementsByTagName("html")
const lang = html.getAttribute("lang");
console.log(lang)

to call getElementsByTagName to get the html element object.

Then we call html.getAttribute with 'lang' to get the lang attribute value of the html element.

Therefore, lang is 'en'.

Conclusion

To read the html element lang attribute value with JavaScript, we can use the getElementsByTagName and getAttribute methods.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *