admin管理员组

文章数量:1241009

I am working rtl right to left direction. I want to know how to get dir value in alert using javascript.

Here is my html code

<html dir="rtl">
</html>

Here is my javascript code

alert(document.getElementsByTagName("html")[0]);

I am not getting the alert as dir ="rtl".

Based on the direction I have to do some condition checking. Instead of attribute is there any way please help me

Please help me how to get that tag name.

I am working rtl right to left direction. I want to know how to get dir value in alert using javascript.

Here is my html code

<html dir="rtl">
</html>

Here is my javascript code

alert(document.getElementsByTagName("html")[0]);

I am not getting the alert as dir ="rtl".

Based on the direction I have to do some condition checking. Instead of attribute is there any way please help me

Please help me how to get that tag name.

Share Improve this question edited Sep 25, 2024 at 11:03 Flimm 151k49 gold badges274 silver badges288 bronze badges asked Mar 3, 2015 at 12:47 Maha1419Maha1419 1911 gold badge2 silver badges11 bronze badges 1
  • Have you tried getAttibute? Something like.. document.getElementsByTagName("html")[0].getAttribute('dir') – Rakesh_Kumar Commented Mar 3, 2015 at 12:49
Add a ment  | 

3 Answers 3

Reset to default 9

Thanks for the reply. Here is what i Did

if (document.dir == "rtl"){
 function name()
}else{
function name1()
}

Regards Mahadevan

This snippet will get data directly from html element

document.getElementsByTagName("html")[0].getAttribute("dir");

you can get this more easily from the attributes of the document Document.dir

var direction;
(document.dir !=undefined)? direction =document.dir : direction =document.getElementsByTagName("html")[0].getAttribute("dir");

if (direction=="rtl") {
//do something
}
else {
//do something else
}

本文标签: How to get the dir attribute of the lthtmlgt element in JavaScript (RTL or LTR)Stack Overflow