user1984805
user1984805

Reputation: 33

How to detect current web page or element direction (rtl/ltr) or any other attribute in javascript?

How can i read any attribute that currently effects an element that is not necessarily style? one such attribute would be "dir".

Upvotes: 3

Views: 5004

Answers (3)

CETb
CETb

Reputation: 372

Also easy to get variable in JavaScript:

var direction = document.dir;

in case of rtl its "rtl"

Upvotes: 1

Stano
Stano

Reputation: 8939

Recently I had similar problem, you can get element's property with window.getComputedStyle and element.currentStyle methods:

    var elem = document.getElementById('test');
    if (window.getComputedStyle) { // all browsers
        cs = window.getComputedStyle(elem, null).getPropertyValue('direction');
    } else {
        cs = elem.currentStyle.direction; // IE5-8
    }
    alert(cs);

jsfiddle ; compatibility info

Upvotes: 9

T.J. Crowder
T.J. Crowder

Reputation: 1074266

These are generally reflected as properties on the element instance. dir is, for example, as are most others. Some have slightly renamed names (htmlFor instead of for, className instead of class), but for the most part it's 1:1 (target for target, action for action, ...).

References:

Upvotes: 0

Related Questions