0

I have a textarea which can take both English and Arabic language.

The English language starts from left to right. So, the cursor should begin in the left.

The Arabic language starts from the right. So the cursor should begin in the right.

How can I change the position of the cursor when the user changes his language? If his language is English the cursor should be on the left and vice-versa.

The user can change his language using ALT + SHIFT in the keyboard.

I have tried this, but it didn't give me any result.

<textarea class="form-control description arabic" id="txt_desc" name="txt_desc" 
          placeholder="Description"></textarea>

.arabic{text-align:right;}

<script type="text/javascript">
    jQuery(document).ready(function($) {
        var reverseFunc = function() {
            $('textarea').each(function() {
                if($(this).hasClass('arabic')) {
                    // do the invert magic...
                    var val = $(this).val();
                    var newVal = val.charAt(val.length-1) + substr(0, val.length-1);
                    $(this).val(val);
                }
            });
        }

        $('textarea').bind('keyup', reverseFunc);
    });
</script>

using shiftkey:

var x = document.getElementById("txt_desc");
    if (event.shiftKey) {
        document.getElementById('txt_desc').style.direction="rtl";
    }else{
         document.getElementById('txt_desc').style.direction="ltr";
    }
mohamad mohamad
  • 613
  • 9
  • 24

4 Answers4

2

check the first character on keyup, if it's Arabic add this direction: rtl;

var la_ar = 
["ي","و","ه","ش","س","ق","ف","غ","ع","ض","ص","ن","م","ل","ك","ظ","ط","ز","ر","ذ","د","خ","ح","ج","ث","ت","ب","ا"]

$("input").keyup(function(){
var la_ar = 
["ي","و","ه","ش","س","ق","ف","غ","ع","ض","ص","ن","م","ل","ك","ظ","ط","ز","ر","ذ","د","خ","ح","ج","ث","ت","ب","ا"]
    var bla = $(this).val();
    var blaـl = bla.charAt(0);
    if($.inArray(blaـl, la_ar) > -1 ){
      //alert('in');
      $(this).css("direction","rtl");


    }else{
      //alert('out');
      $(this).css("direction","ltr");
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="text">
Nrome
  • 131
  • 4
0
 use the css attribute direction

    direction: rtl;

    In Javascript your code add this

    var x = document.getElementById("txt_desc");
        if (event.shiftKey) {
            document.getElementById('txt_desc').style.direction="rtl";
        }else{
             document.getElementById('txt_desc').style.direction="ltr";
        }
0

You have to use Html5 property this will work.

.arabic{ direction: rtl;}
Purvik Dhorajiya
  • 4,662
  • 3
  • 34
  • 43
Mehraj Khan
  • 927
  • 6
  • 17
0

You can try this:

<textarea style="direction: rtl"></textarea>
Nick is tired
  • 6,860
  • 20
  • 39
  • 51
Mehraj Khan
  • 927
  • 6
  • 17