21

Suppose I have an HTML document that looks like this:

<html lang="en"> ... </html> 

My question is: how to get the value of the attribute lang with jQuery?
I've tried $("html").attr("lang") but it did not work... any suggestions?

5
  • 2
    Cannot reproduce. $("html").attr("lang") works just fine for me when extrapolating from the example you have given. Can you post more details? Commented Jul 27, 2009 at 10:21
  • check if all your scripts are running (with firebug - see if no error); and also your html is valid. Commented Jul 27, 2009 at 10:24
  • May be this is a HTML vs. XHTML issue and lang attribute just don't end up in the DOM for one of them. Needs more research. Commented Jul 27, 2009 at 10:26
  • 1
    I've tried that again with an empty html document (just <html>, jquery include and the js script) and it work. but when I try to execute the same script on a complex page (gmail inbox using GreaseMonkey) it returns nothing ... Commented Jul 27, 2009 at 10:58
  • $("html").attr("lang") is working in major browser with html5 in 2015! Commented Nov 16, 2015 at 9:57

3 Answers 3

25

Access the attribute directly, ex :

$('html')[0].lang 
Sign up to request clarification or add additional context in comments.

Comments

13

You don't need to use jQuery for this.

The easiest way to retrieve the lang attribute is to access the lang property on the read-only documentElement object:

document.documentElement.lang; 

Comments

9

Use .attr()

alert($('html').attr('lang'));

1 Comment

I have vote myself down. I missed the part where he said what he did already.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.