8

I have a TEXTAREA control (ID="taCode"), maxlength=400, that is pre-filled with 400 whitespaces.

I use Javascript to force insert-mode (replace) when entering text into textarea:

var input = document.getElementById("taCode");
input.addEventListener('keypress', function(){
    var s = this.selectionStart;
    var e = this.selectionEnd;
    this.value = this.value.substr(0, s) + this.value.substr(e + 1);

    if (this.value.length < 399)
    {
        this.value += new Array(399-this.value.length).join(' ');
    }

    this.selectionEnd = this.selectionStart = s;
}, false);

HTML:

<TEXTAREA ID="taCode" COLS="80" ROWS="5" MAXLENGTH="400" style="overflow:hidden">                                                                                                                                                                                                                                                                                                                                                                                                                </TEXTAREA>

Everything works fine using a desktop and mouse, but trying to place the cursor inside the TEXTAREA using a mobile (Iphone) fails (EDIT: Placing cursor on first row works sometimes) and nothing can be typed into the field, except for on the first line. Any idea of how to get it to work for mobiles for all lines?

Test: http://artificial.se/ta.html

Peter Wirdemo
  • 498
  • 2
  • 11
  • 25

4 Answers4

2

Your first line in iPhone is completely populated because of spaces.So, when you put your cursor there and try to type, it moves on to next line so as to accommodate all the spaces which are prepopulated by you.If you clear those spaces and try to type on iPhone or reduce the number of spaces and try it.It would definitely work. If not then add word-break:break-word property to your textarea element to make it work.

  • word-break:break-word didn't work and I have the textarea prefilled with spaces for a reason. It works without it being prefilled with 400 spaces, but that is not the solution I am looking for. I need to get it to work placing the cursor "inside" a line full of empty spaces. – Peter Wirdemo Sep 19 '17 at 11:19
  • use it along with other properties on textarea as: textarea{ white-space: pre-wrap; word-wrap: break-word; word-break: break-all;} – user8335505 Sep 19 '17 at 20:52
2

Try something like:

area.addEventListener("input", function(e) {
    const caretIndex = area.selectionStart;
    const content = area.value;
    const newContent = str.slice(0, caretIndex) + e.key + str.slice(4);
    area.value = newContent;
});

This will likely need a little work still. Some combination of input/change/keydown should work. I'd also suggest making the number of spaces one less than you need, and later fill the end with an extra space when you are using the entered value.

Gibolt
  • 42,564
  • 15
  • 187
  • 127
  • Not sure if you suggest using your function in addition to mine or instead of. I tried both combinations without success: http://artificial.se/ta2.html still cannot place the cursor on any other row than the first using IPhone/Safari. I am currently trying to create a workaround using 5 separate one-line textareas where one can "jump" between them using Arrow key UP/DOWN from client with keyboard. – Peter Wirdemo Sep 21 '17 at 10:58
1

may be you can use the keydown event for this (sorry i just want to comment but my reputation is low)

newbie
  • 334
  • 2
  • 8
0

I suggest that if you want spaces appended you can get the remaining spaces appended in form submit, working with js in this can be avoided and will be better after user has filled the input field.

JosephC
  • 166
  • 1
  • 7
  • You are right about that but it's actually the user interface - being able to place the cursor anywhere and add content there (similar to a painting application) - that I am trying to create/mimic. – Peter Wirdemo Sep 22 '17 at 13:18