Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to obtain lang attribute in HTML using JavaScript?

How to obtain lang attribute in HTML using JavaScript?

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
like image 831
Gordian Yuan Avatar asked Jun 04 '09 08:06

Gordian Yuan


People also ask

How add lang attribute in HTML?

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.

What is lang property in JavaScript?

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.

Is lang an attribute in HTML?

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.

How can get HTML lang attribute value in jquery?

$("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.


2 Answers

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 
like image 107
Christoph Avatar answered Oct 03 '22 18:10

Christoph


Just.

document.getElementsByTagName('html')[0].getAttribute('lang'); 

And with the namespace

document.getElementsByTagName('html')[0].getAttribute('xml:lang'); 
like image 28
Christophe Eblé Avatar answered Oct 03 '22 19:10

Christophe Eblé