Right to left Text HTML input
Asked Answered
I

9

48

For my website, i need to provide arabic support. Part of it is to provide input textboxes where when user types in, the new characters have to be appended to the left and the text has to be right aligned.

setting the css property to

text-align:right

didn't work, as i could not get the cursor to come to the left and add letters there. So I removed that property and added

direction:RTL

Here, the cursor came to the left and text was right aligned. but the newly added characters were not getting appended to the left. Instead they were getting appended to the right end only.

How do I fix this? please help..

For example, see the google arabic page search box. I need the exact behavior, although not with those fancy keyboard icon etc., http://www.google.com/webhp?hl=ar

Invite answered 23/9, 2011 at 5:57 Comment(2)
Normally <bdo dir="rtl">TEXT HERE</bdo> would do exacly that, but when receiving text from a form or such, it won't work.Marduk
Could you post your solution?Whetstone
E
40

Here's what I can think of:

  • Use direction:RTL for the RIGHT alignment
  • Write a JavaScript handler attached to the event: "onkeyup", which performs the shifting of the entered character to the LEFT (doing some text processing).
Expound answered 23/9, 2011 at 6:11 Comment(3)
Ahmed can you please share the way you managed to work it out?Whetstone
A fiddle would be nice. ThanksZara
JS is not needed in any way, sorry but this is a really bad suggestion. Nicholas answer below, dir="rtl" is all that is needed: w3.org/International/questions/qa-html-dirSassy
C
64

You can use the dir="rtl" on the input. It is supported.

<input dir="rtl" id="foo"/>
Clarsach answered 8/8, 2014 at 17:49 Comment(1)
Also note though that CSS shims are still recommended as of 2018 for some browsers, see w3.org/International/articles/inline-bidi-markupSassy
E
40

Here's what I can think of:

  • Use direction:RTL for the RIGHT alignment
  • Write a JavaScript handler attached to the event: "onkeyup", which performs the shifting of the entered character to the LEFT (doing some text processing).
Expound answered 23/9, 2011 at 6:11 Comment(3)
Ahmed can you please share the way you managed to work it out?Whetstone
A fiddle would be nice. ThanksZara
JS is not needed in any way, sorry but this is a really bad suggestion. Nicholas answer below, dir="rtl" is all that is needed: w3.org/International/questions/qa-html-dirSassy
P
9
function rtl(element)
{   
    if(element.setSelectionRange){
        element.setSelectionRange(0,0);
    }
}




<input type="text" name="textbox" style="direction:RTL;" onkeyup="rtl(this);"/>

This code will do.

Playtime answered 29/9, 2011 at 16:24 Comment(0)
R
8

Simply use this CSS, this will change your text field and cursor position from right to left.

input, textarea { 
 unicode-bidi:bidi-override; 
 direction: RTL; 
}
Rickets answered 13/5, 2020 at 9:7 Comment(1)
Using unicode-bidi is not recommended by MDN - developer.mozilla.org/en-US/docs/Web/CSS/unicode-bidiWescott
B
5

Use only direction:RTL and when switched to a proper keyboard (e.g. Arabic) in the system settings, the newly added characters will correctly be appended to the left.

Barboza answered 25/7, 2014 at 15:40 Comment(0)
L
5

A feature specific to Angular Material, in addition to direction: rtl, is :

.mat-form-field {
   text-align: start!important;
 }

This will work for both RLT & LTR

Luke answered 5/7, 2018 at 11:4 Comment(0)
W
5

A better, more user-friendly way to do this is to set the dir attribute to auto.

<input dir="auto" id="foo"/>

This way if a user enters English text it will be left-aligned and if he enters Arabic text it will be right-aligned automatically

See here for more information on the dir attribute

Wescott answered 17/9, 2021 at 16:56 Comment(0)
T
3

Use on the input in css.

input {
  unicode-bidi:bidi-override;
  direction: RTL;
}
Theoretician answered 21/12, 2016 at 7:39 Comment(1)
Using unicode-bidi is not recommended by MDN - developer.mozilla.org/en-US/docs/Web/CSS/unicode-bidiWescott
G
-1

It works for Chrome browser.
Use a div element and make it editable.

    <div contenteditable="true">
    </div>
Gobetween answered 11/8, 2019 at 9:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.