1

I've tried to solve this by my own but I've been struggling a lot. I've got 5 inputs that are stick together (they actually look like a textarea), and what I'm trying to do, is to fix a limit of the number of characters in each input, and when we reach the char limit, the cursor automatically focus on the next input and paste the last word that we couldn't entirely write in the last input before and if we erase few characters of this word, its goes back to the previous input (only if the characters limits isn't reached). Here's what I tried to do:

function passage(enCours, suivant, limite){

      if (enCours.value.length == limite)
        {
          $('#cmzTextLines'+suivant).focus();
        }
}

function test(now, suivant){
    var text = $('#cmzTextLines'+now).val();
    var textSuivant = $('#cmzTextLines'+suivant).val();
    //var lines = text.split("\n");
      for (var i = 0; i < text.length; i++) {
        if (text[i].length <= maxLength) continue;
        var j = 0; space = maxLength;
        while (j++ <= maxLength) {
          if (text[i].charAt(j) === " ") space = j;
        }
       textSuivant = text[i].substring(space + 1) + (textSuivant || "");
       text[i] = text[i].substring(0, space);
      }
}

The HTML:

<input type="text" class="form-control" id="cmzTextLines1" name="cmzTextLines1" onkeyup="passage(this, 2, 30); test(1, 2);">

<input type="text" class="form-control" id="cmzTextLines2" name="cmzTextLines2"
 onkeyup="passage(this, 3, 30); test(2, 3);">
  • Possible duplicate of [Focus next input once reaching maxlength value](http://stackoverflow.com/questions/15595652/focus-next-input-once-reaching-maxlength-value) – Clyde Lobo Jun 28 '16 at 14:39
  • 1
    Yes, that actually working on my code, but what im not able to do, is to cut the last word for example, let says that i've been writting "good morning" and that the input only hold 8 characters, so i wanna have "good" in the first input and "morning in the second" and not "good mor" "ning". – Nouaman Oumallani Jun 28 '16 at 14:48

1 Answers1

0

JSFiddle: https://jsfiddle.net/fumm44f3/2/

var replacedval = rawval.substring(0, rawval.length - lastwordlength);

$(this).val(replacedval);
$(this).next(".form-control").val(lastword);

It'd be simple enough from here to check to see if an input has 0 characters on backspace as it already checks for backspace, and then go to prev input if so.

tazius
  • 11
  • 1