How to obtain lang
attribute in HTML using JavaScript?
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
Always add a lang attribute to the html tag to set the default language of your page. If this is XHTML 1. x or an HTML5 polyglot document served as XML, you should also use the xml:lang attribute (with the same value). If your page is only served as XML, just use the xml:lang attribute.
The lang property sets or returns the value of an element's lang attribute. The lang attribute specifies the element's language code, like "en" for English, "es" for Spanish, or "fr" for French.
HTML lang AttributeThe lang attribute specifies the language of the element's content. Common examples are "en" for English, "es" for Spanish, "fr" for French, and so on.
$("html"). attr("lang") works just fine for me when extrapolating from the example you have given. Can you post more details? check if all your scripts are running (with firebug - see if no error); and also your html is valid.
If both attributes agree on their values (as they should), it's enough to read either of them. I'd suggest using
document.documentElement.lang
Just.
document.getElementsByTagName('html')[0].getAttribute('lang');
And with the namespace
document.getElementsByTagName('html')[0].getAttribute('xml:lang');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With