I sometimes need to find out the computed value of the HTML lang attribute (the element's language). I'm using jQuery. For example, in the following code I'd like to know the computed lang of the <p> element, and I would expect the value he:
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8" />
<title>lang test</title>
</head>
<body>
<div lang="ar">
<div lang="he">
<p>Hallo.</p>
</div>
</div>
</body>
</html>
Doing $( 'p' ).attr( 'lang' ) returns nothing, probably because the <p> element doesn't have its own lang attribute.
This code seems to do the right thing:
$( 'p' ).closest( '[lang]' ).attr( 'lang' )
Is this functionally correct? And is there a better way to do it in jQuery, in pure JavaScript or with another JavaScript library?
Thank you.