0

Please see the image. There are 7 text boxes where only one character can be entered .

4 conditions are to be fulfilled

  1. The last text box - the rightmost/seventh textbox will be input first, then the sixth one will be filled , then the fifth and so on

  2. Then the rightmost/seventh textbox value will shift (left shift) to the sixth and in this way values will shift until all 7 fields are filled

  3. If we place the cursor on any other element except the last/seventh/rightmost it will move the cursor to the rightmost .

  4. There will be backspace function which will delete the rightmost, ie. the the seventh field will be deleted the first field value will move to second, second to third , sixth to seventh , like this , a right shift will occur in this way all elements are to be deleted

The entire solution should be in Javascript , no JQuery can be used

https://i.stack.imgur.com/dISMA.jpg

Please refer the image above

Javascript Code

var myInputs = document.getElementsByTagName("input");
var myEditable = document.getElementById("seventh");
for (var i = 0; i < myInputs.length; i++) {
 myInputs[i].addEventListener("click", function() {
 document.getElementById("seventh").focus();
 })
}

myEditable.addEventListener("keydown", function(evt) {
 /****
   *  A few things are handled here: we can check if
   *  the input is a numeric, and we can check if the input
   *  is a backspace. Nothing else is allowed.
  ****/
  if (evt.which == 8) {
  // If a backspace has been pressed, move all the input
  //  values one field UP.

      myEditable.blur();
     for (var i = myInputs.length - 1; i >= 1; i--) {
     myInputs[i].value = myInputs[i - 1].value;
    }
    myInputs[0].value = "";
   } else if (evt.which >= 48 && evt.which <= 59) {
    // Here, we have a number. Everything gets shifted to the LEFT

    if (myInputs[0].value == "") {
     for (var i = 0; i < myInputs.length - 1; i++) {
      myInputs[i].value = myInputs[i + 1].value;
      }
     myEditable.value = String.fromCharCode(evt.which);
   }
  } else {
  console.log("You did something else...");
 }
})

HTML Code

 <form>
<input type="text" id="first" size="1" maxlength="1" />  
<input type="text" id="second" size="1" maxlength="1" />  
<input type="text" id="third" size="1" maxlength="1" />
<input type="text" id="fourth" size="1" maxlength="1" />
<input type="text" id="fifth" size="1" maxlength="1" />
<input type="text" id="sixth" size="1" maxlength="1" />
<input type="text" id="seventh" size="1" maxlength="1" />  
</form>

In this code there are two problems

  1. First it is working in JSBin - https://jsbin.com/duxogezake/edit similarly it is working in Fiddle but not in Chrome 55 or any other browser But it should work in chrome by any means

  2. When we are using backspace the cursor should remain in the last/rightmost/seventh text box but the cursor is not remaining - we have to place the cursor again & again in the last text box to do the operation (read the fourth condition in the top before )

mdv
  • 9
  • 4
  • 4
    Your code is probably running **before** the DOM for your `
    ` is parsed. JSBin puts your code in a `
    – Pointy Jan 25 '17 at 02:26
  • 2
    Check your Chrome developer console for error details. – ceejayoz Jan 25 '17 at 02:27
  • @Pointy it is also working in fiddle , so whats the workaround. It should work in Chrome 55 – mdv Jan 25 '17 at 02:28
  • I opened your JSBin link both in Chrome and Opera. The "rules" you specified for the cursor to be always at the rightmost input field is working fine, but the "shifting of input values" does not. All it allows me to do is input a single character at the rightmost field. After that, no more inputs are accepted. – ITWitch Jan 25 '17 at 02:29
  • @Pointy Uncaught TypeError: Cannot read property 'addEventListener' of null in this line - myEditable.addEventListener("keydown", function(evt) – mdv Jan 25 '17 at 02:29
  • @ITWitch Uncaught TypeError: Cannot read property 'addEventListener' of null in this line - myEditable.addEventListener("keydown", function(evt) – mdv Jan 25 '17 at 02:30
  • Yup, premature execution. In jQuery you'd wrap your whole code in `$.ready(function() { ... })`. In non-jQuery... why did you mark your question [tag:jquery]?!? See [here](http://youmightnotneedjquery.com/#ready) how it's done. Or move your script to the bottom of the page. – Amadan Jan 25 '17 at 02:31
  • 3
    @mdv yes that's precisely what would happen. JSFiddle puts your JavaScript code (by default) in a window "load" event handler, and so also avoids the same problem. – Pointy Jan 25 '17 at 02:32
  • @Amadan whats the solution can you please illustrate in a new Fiddle – mdv Jan 25 '17 at 02:33
  • @Pointy so whats the solution , can please tell the workaround – mdv Jan 25 '17 at 02:34
  • Pointy is correct, place script under html like http://codepen.io/anon/pen/ggxmax – Jonathan Niu Jan 25 '17 at 02:36
  • @Jonathan Niu ok but please fix the backspace problem , that is also a big problem , cursor should always stay in the last/seventh box – mdv Jan 25 '17 at 02:38
  • click the link again please – Jonathan Niu Jan 25 '17 at 02:43
  • @Jonathan Niu right shifting not working properly you observe see - it should work as my old code where the cursor was not staying – mdv Jan 25 '17 at 02:46
  • @mdv I don't understand, It was working as your original code before but you said there was a backspace problem. I reverted it to the original. Please be more specific on what the problem is – Jonathan Niu Jan 25 '17 at 02:50
  • posted an answer below. let me know if it works. – ITWitch Jan 25 '17 at 03:29
  • @Jonathan Niu 1 final question I want to shift the focus from right to left as the values are entered and shift the focus from left to right as the numbers are deleted – mdv Jan 26 '17 at 05:21
  • @ITWitch 1 final question I want to shift the focus from right to left as the values are entered and shift the focus from left to right as the numbers are deleted . I mean values can be only entered from the right and as new values are entered it is shifted to the left . Everything is OK . Just the thing is when the value will move to sixth from seventh the sixth one should remain focus , right now the seventh one is remaining focused , i mean during insertion the cursor will in the seventh but the focus should move from right to left one by one – mdv Jan 26 '17 at 05:27
  • @Jonathan Niu 1 final question I want to shift the focus from right to left as the values are entered and shift the focus from left to right as the numbers are deleted .I mean values can be only entered from the right and as new values are entered it is shifted to the left . Everything is OK . Just the thing is when the value will move to sixth from seventh the sixth one should remain focus , right now the seventh one is remaining focused , i mean during insertion the cursor will in the seventh but the focus should move from right to left one by one – mdv Jan 26 '17 at 05:28
  • @Pointy 1 final question I want to shift the focus from right to left as the values are entered and shift the focus from left to right as the numbers are deleted .I mean values can be only entered from the right and as new values are entered it is shifted to the left . Everything is OK . Just the thing is when the value will move to sixth from seventh the sixth one should remain focus , right now the seventh one is remaining focused , i mean during insertion the cursor will in the seventh but the focus should move from right to left one by one – mdv Jan 26 '17 at 05:29
  • Are you referring to `focus` and `cursor` location as different things? As far as I know, whichever your focus is, the cursor is automatically placed there, too. What do you mean by keeping the cursor on the seventh but moving the focus on the sixth? Please enlighten us. – ITWitch Jan 27 '17 at 03:36

1 Answers1

0

TRY THIS: Press the "run code snippet" below and let me know if it works on your end.

I tested this in my localhost. It is working fine both in my Chrome and Opera, and the cursor is now staying in the rightmost input field.

Although, you seem to have other bugs in your code, because the texts "Ck" and "Dv" are appearing when the input values are shifted. (EDIT: nevermind. I think it's because I entered some characters instead of numbers, which you seemed to have restricted.)

var myInputs = document.getElementsByTagName("input");
    for (var i = 0; i < myInputs.length; i++) {
      myInputs[i].addEventListener("click", function() {
        document.getElementById("seventh").focus();
      })
    }
    
    /*wrap your code inside window.load to prevent premature executuion*/
    window.onload = function() {
    console.log('window has loaded');
      var myEditable = document.getElementById("seventh");
      myEditable.addEventListener("keydown", function (evt) {
        /****
         *  A few things are handled here: we can check if
         *  the input is a numeric, and we can check if the input
         *  is a backspace. Nothing else is allowed.
         ****/
        if (evt.which == 8) {
          // If a backspace has been pressed, move all the input
          //  values one field UP.
          myEditable.blur();
          for (var i = myInputs.length - 1; i >= 1; i--) {
            myInputs[i].value = myInputs[i - 1].value;
          }
          myInputs[0].value = "";
        } else if (evt.which >= 48 && evt.which <= 59) {
          // Here, we have a number. Everything gets bumped to the LEFT
    
          if (myInputs[0].value == "") {
            for (var i = 0; i < myInputs.length - 1; i++) {
              myInputs[i].value = myInputs[i + 1].value;
            }
            myEditable.value = String.fromCharCode(evt.which);
          }
        } else {
          console.log("You did something else...");
        }
    
        /*keep the cursor on the seventh input field right after hitting backspace and shifting values*/
        myEditable.focus();
    
      });
    
    }
<form>
<input type="text" id="first" size="1" maxlength="1" />  
<input type="text" id="second" size="1" maxlength="1" />  
<input type="text" id="third" size="1" maxlength="1" />
<input type="text" id="fourth" size="1" maxlength="1" />
<input type="text" id="fifth" size="1" maxlength="1" />
<input type="text" id="sixth" size="1" maxlength="1" />
<input type="text" id="seventh" size="1" maxlength="1" />  
</form>
ITWitch
  • 1,449
  • 3
  • 19
  • 35