Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change CSS direction property of the input field automaticly if the user can use an language rtl or ltr

Example: if I use arabic language the text field direction will be rtl and if I want to write a new text and I switch to the English language the direction inside the text field (`text-align: left) will be ltr automatically

like image 539
Kheir Eddine Avatar asked Jul 21 '11 13:07

Kheir Eddine


People also ask

How do I use rtl in CSS?

The direction CSS property sets the direction of text, table columns, and horizontal overflow. Use rtl for languages written from right to left (like Hebrew or Arabic), and ltr for those written from left to right (like English and most other languages).

How do you convert rtl to LTR?

Shortcuts: Ctrl+Shift+J -- switch to RTL Ctrl+Shift+E -- switch to LTR Note 1: If one or both of the shortcuts don't work, it means that something else is mapped to them.

Which of the following CSS text property changes the direction of text?

The direction property specifies the text direction/writing direction within a block-level element. Tip: Use this property together with the unicode-bidi property to set or return whether the text should be overridden to support multiple languages in the same document.

How do you change rtl to HTML?

Setting up a right-to-left pageAdd dir="rtl" to the html tag any time the overall document direction is right-to-left (RTL). This sets the default base direction for the whole document. All block elements in the document will inherit this setting unless the direction is explicitly overridden.


2 Answers

You could use the global HTML5 attribute dir with a value of auto here, like so:

<input type="text" dir="auto" />

From the specification:

The auto keyword, which maps to the auto state Indicates that the contents of the element are explicitly embedded text, but that the direction is to be determined programmatically using the contents of the element (as described below).

Note: The heuristic used by this state is very crude (it just looks at the first character with a strong directionality, in a manner analogous to the Paragraph Level determination in the bidirectional algorithm). Authors are urged to only use this value as a last resort when the direction of the text is truly unknown and no better server-side heuristic can be applied.

http://www.w3.org/TR/html5/elements.html#the-dir-attribute

As this quote suggests, it would be better to find out on the server side which direction should be used, but you can use this if you have no way of knowing it.

like image 116
MFTSBU Avatar answered Oct 22 '22 17:10

MFTSBU


thanks for you help, I used javascript to resolve this problem , in this code I used jquery framework and it work only for Arabic language , for others language you must edit charCodeAt(0) comparison value.

$('#input_ar').keyup(function(){
    if ((($(this).val().charCodeAt(0) > 0x600) && ($(this).val().charCodeAt(0) < 0x6FF)) || ($(this).val()=="")) { $('#search_input').css("direction","rtl"); }
    else { $('#search_input').css("direction","ltr"); }
});
like image 20
Kheir Eddine Avatar answered Oct 22 '22 16:10

Kheir Eddine