Transfer information to the following section once the current one has been completed - Utilizing JavaScript tabulation

I'm facing an issue with my script that involves input data into a div field where each div should automatically move to the next one when it reaches a certain character limit.

While the script works well when using inputs, I encountered a problem when trying to input data via a virtual keyboard and not being able to use focus. I attempted to input data to specific divs based on their classes, but switching classes once a div is full did not produce the desired result.

var a = document.getElementById("a"),
    b = document.getElementById("b"),
    c = document.getElementById("c");
var $write = $(".active");

$('#keyboard').click(function() {
    var checka = $('#a').html().length;
    var checkb = $('#b').html().length;

    if (checka === 4) {
        a.removeClass("active");
        b.addClass("active");
    } else if (checkb === 4) {
        b.removeClass("active");
        c.addClass("active");
    }

    var $this = $(this),
        character = $this.html();
    $(".active").html($write.html() + character);
});

$('.delete').click(function() {
    var html = $write.html();
    $write.html(html.substr(0, html.length - 1));
    return false;
});

You can view the demo here: http://jsfiddle.net/4m5fg/456/

Answer №1

Take a look at this code snippet

$(".keyboard").on("click", function() {

  $(".block").removeClass("active").filter(function() {
    return this.innerText.length < 4;
  }).first().addClass("active").append(this.innerText);

});

$(".delete").on("click", function() {

  var inputs = $(".block").removeClass("active").filter(function() {
    return this.innerText.length > 0;
  }).last().addClass("active").html(function() {
    return this.innerText.substr(0, this.innerText.length - 1);
  });

});

If you are creating a virtual keyboard, it is recommended to use class instead of id. This allows for easier selection and manipulation of elements like .keyboard.

By using this.innerText.length, you can determine if the input is complete and customize it dynamically by changing the 4 with an attribute.

To manage the 'active' class, remove it from all .blocks before adding it to the current block when needed.

For the 'DEL Key', identify the last block with text and remove the last character accordingly.

EDIT: Check out the updated example with active class

Answer №2

Your code has several issues that need to be addressed:

  1. In order to use the .removeClass and .addClass functions, you should have a jQuery object instead of a Node element.
  2. If the first div already contains 4 A, the checka value will remain 4, so it's important to also check if checkb == 0.
  3. Make sure to retrieve the current .active each time instead of just on page load. Placing var $write = $(".active"); inside the click event is necessary.

var a = $("#a"),
    b = $("#b"),
    c = $("#c"),
    $write;

$('#keyboard').click(function(){
  var checka = $('#a').html().length;
  var checkb = $('#b').html().length;

  if (checka === 4 && checkb === 0) {
    a.removeClass("active");
    b.addClass("active");
  }
  else if (checkb === 4) {
    b.removeClass("active");
    c.addClass("active");
  }

  $write = $(".active");

  var $this = $(this),
      character = $this.html();

  $(".active").html($write.html() + character);
});

$('.delete').click(function() {
  var html = $write.html();
  $write.html(html.substr(0, html.length - 1));
  
  if ($write.html().length == 0) {
      $write = $write.prev();
  }
  
  return false;
});
.block {
  background: #fff; 
  color: red; 
  border: 0; 
  width: 45px; 
  height: 20px; 
  padding: 10px; 
  float: left; 
  margin: 5px;
}

input:focus, textarea:focus {
  outline: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="a" class="block active" ></div>
<div id="b" class="block active2"  ></div>
<div id="c" class="block" ></div>

<div id="keyboard">A</div>
<span class="delete">DEL</span>

Answer №3

Check out this revised code to fix the errors:

var a = $("#a"),
    b = $("#b"),
    c = $("#c");
    var $write = $(".active");

$('#keyboard').click(function(){

  var checka = $('#a').html().length;
  var checkb = $('#b').html().length;

  if (checka === 4) {
    a.removeClass("active");
    b.addClass("active");
    $write = b;
  }
  if (checkb === 4) {
    b.removeClass("active");
    c.addClass("active");
    $write = c;
  }

  var $this = $(this),
      character = $this.html();
  $(".active").html($write.html() + character);


});
$('.delete').click(function() {
    var html = $write.html();
  if (html.length == 0) {
    if ($write.is('#c')) {
        $write.removeClass('active');
        $write = b;
      $write.addClass('active');
    } else if ($write.is('#b')) {
        $write.removeClass('active');
        $write = a;
      $write.addClass('active');
    }
    html = $write.html();
  }
  $write.html(html.substr(0, html.length - 1));
  return false;
});

JSFIDDLE

Answer №4

<app-step1 class="star-inserted">
  <div c79="" class="form-group mt-2 star-inserted">
      <ng-otp-input c79="" _nghost-gtc-c25="">
        <input id="" type="text" class="otp-input" c25="" maxlength="1" autocomplete="off" pattern="\d*" charCodeLimit='[48,57]'>
        <input id="" type="text" class="otp-input" c25="" maxlength="1" autocomplete="off" pattern="\d*" charCodeLimit='[48,57]'>
        <input id="" type="text" class="otp-input" c25="" maxlength="1" autocomplete="off" pattern="\d*" charCodeLimit='[48,57]'>
        <input id="" type="text" class="otp-input" c25="" maxlength="1" autocomplete="off" pattern="\d*" charCodeLimit='[48,57]'>
        <input id="" type="text" class="otp-input" c25="" maxlength="1" autocomplete="off" pattern="\d*" charCodeLimit='[48,57]'>
        <input id="" type="text" class="otp-input" c25="" maxlength="1" autocomplete="off" pattern="\d*" charCodeLimit='[48,57]'>
      </ng-otp-input>
    </div>
  </div>
</app-step1>

Moreover, the following functions have been added:

var container = document.getElementsByClassName("star-inserted")[0];
container.onkeyup = function(e) {
  var target = e.srcElement;
  if (target.value.length >= target.maxLength) {
    var next = target;
    while (next = next.nextElementSibling) {
      if (next == null)
        break;
      if (next.tagName.toLowerCase() == "input") {
        next.focus();
        next.setSelectionRange(0, next.value.length);
        break;
      }
    }
  }
}
container.onkeypress = function(e) {
  let charCodeLimit = e.target.getAttribute("charCodeLimit");
  if (charCodeLimit) {
    let limit = JSON.parse(charCodeLimit);
    if(limit.length==2){
      return (e.charCode >= limit[0] && e.charCode <= limit[1]) || e.charCode == 0;
    }
  }
}
container.onclick = function(e) {
  if (e.target.tagName.toLowerCase() == "input") {
    e.target.select();
  }
}

To view an Online Demo for this code snippet, visit: https://jsfiddle.net/nsvbtkzq/

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

the parameter is absent or its value is null for:

I have a POST request from an api I am looking to simply send a request to the API without saving any data in my application and receive an ok response. Here is the script I am using: <script type="text/javascript"> $('input[type=submit]&apos ...

Leveraging the power of THREE.js TransformControls

I am currently working on a project in JSBin and attempting to incorporate the THREE TransformControls class. Due to constraints, I am unable to share my full code, but my JavaScript is contained within <script type="text/javascript"></s ...

conceal the act of pushing

After adding a property in a push function to toggle between hiding the label and showing the input, there is a need to reverse this action when the user clicks on Save changes. This can be achieved by using the ng-hide directive. <tr ng-repeat="pe ...

Difficulty in finding and retrieving array indexes that match a specific character pattern

Is it possible to return indexes from an array based on a search term, regardless of the order of characters in the array? const array = ['hell on here', '12345', '77788', 'how are you llo']; function findMatchingI ...

After the completion of progress, display the POST outcome

One feature I have implemented is a progress bar that displays the progress made when a user inputs a value into a form and submits it. Based on this input value, a query is executed by the system a specified number of times to add records, which are then ...

Utilizing fibrous in node.js to efficiently fetch data from the request library

I am struggling to efficiently utilize the fibrous library in conjunction with request for obtaining the body of an HTTP request synchronously. However, I have encountered difficulties in managing the flow. In order to address this issue, I created a simp ...

Using a Javascript loop to showcase values from a database

As a Python developer who is new to JavaScript and AJAX, I am currently working on creating a pie chart that displays database values. $(document).ready(function () { google.charts.load('current', { 'packages': ['corechart&ap ...

Audio file is playing on only one computer and not the other

I'm facing an issue with embedding a sound file on a website. It works perfectly fine on one computer, but on another, I encounter an 'ERROR' message instead of the sound playing. Any idea why this discrepancy is happening? Here's the ...

Adjust the background color of the body content to reflect changing content

How can I change the background color to #97EFFC when a menu item is selected? I want the body content background to be transparent until a menu item is displayed. Site.css /* Responsive: Portrait tablets and up */ @media screen and (min-width: 768px) { ...

Using AngularJS to bind radio buttons to ng-model

Here is a snippet of my HTML code where I attempted to bind the "formFriendsAll" scope to the ng-model. <form ng-submit="submitForm()" > <div class="col-sm-3"> <div class="form-group"> <label>Which Persons to show?< ...

When using jQuery's $.ajax function, the success parameter with the textStatus is being disregarded without any indication

I'm confused about what's going on here. The AJAX request is being successfully posted and Firebug isn't showing any errors. However, the success function {alert("complete")} never seems to fire. It's as if it's being ignored. I ...

Adjust the text within the paragraph dynamically according to the option chosen in the drop-down menu using

I'm having trouble updating a paragraph in a letter based on the user's selection from a dropdown menu. I can't seem to figure it out. I don't know whether ng-show/hide or ng-options is the best approach for this. I feel completely los ...

Interactive pop-up window featuring conversational chat boxes

Trying to create a comment box within a Modal dialog box that is half of the width. The comments in this box should be read-only and created using div tags. Attempted reducing the width and using col-xs-6, but ending up with columns spanning the entire w ...

Steps to deactivate two choices in a multi-select dropdown menu and visually dim those options

Hey there, I recently worked with Angular8 and Bootstrap 4. I utilized a Bootstrap multi-select dropdown, where I encountered an issue: specifically, I'm trying to disable and gray out the options for PL Marketing and CL Marketing but have been unsucc ...

Guide on adding user input values (type=text) into HTML using JavaScript function outcome

I'm looking for a way to use the output of a JavaScript function as the value for an input field in HTML. Currently, I have a JavaScript function that generates a random string: function generateRandomString(length) { let result = ''; ...

Alter the display style of the nearest span using jQuery when the select option is modified

Here is the code block that I am working with: <div class="user-select-dropdown"> <span class="filter-header">User:</span> <span class="remove-option">✖</span> <select class="form-control" ng-model="filter.Us ...

CSS only accordion divs that are all opened by default with no need for jquery

My current setup involves: jsfiddle.net/wromLbq5 I would like to enable the functionality of having multiple accordion sections open simultaneously upon page load. This means that when one section is opened, the others should not close. Is there a way to ...

How to change a CSS 'left' property using Jquery or Javascript

Upon examining my DOM, I found the following element: <div id="itemEditor" class="quoteItemEditorView partType_MATERIAL editMode selectorEnabled" style="left: -1px; right: 0px; width: auto; min-width: 480px; display: block;" > I have b ...

Unusual behavior experienced with raycasting in Three JS when objects are are repositioned

Software Versions THREE.ObjectLoader2: 2.4.1 THREE.LoaderSupport.MeshBuilder: 1.2.1 THREE.LoaderSupport.WorkerSupport: 2.2.0 THREE.WebGLRenderer: 93 THREE.REVISION: 93 Anomalies Discovered During a raycast operation on objects within my scene, I encount ...

The issue with Cordova (PhoneGap) is that when you move a video file captured by the camera, the gallery thumbnail

Is there a plugin available for Cordova (Android) that can refresh the gallery? I am currently capturing video using the Cordova plugin cordova-plugin-media-capture, which saves the video to the default sdcard path (gallery). After saving the file, I ...