Keyboard hack

by colintoh

HTML

<div class="text"></div>
<span class="hint">u</span>
<div class="text"></div>
<div class="text"></div>
<div class="text"></div>
<br>
    <br>
        <br>
<input>

CSS

.text{
    float: left;
    color: black;
    font-size: 25px;
    width: 40px;
    height: 30px;
    text-align:center;
    border-bottom:1px solid #000;
    margin-right: 5px;
}

.hint {
    float: left;
    width: 40px;
    height: 30px;
    text-align:center;
    font-size: 25px;
}

.active{
    border-bottom:1px solid red;
}

JavaScript

var index = 0;
var wordLength = $('.text').length;

$('.text').on('click',function(){
    index = $('.text').index(this);
    $('.text').removeClass('active');
    $(this).addClass('active');
    $('input').trigger('touchstart');
});

$('input').on('touchstart',function(){
     $(this).focus();
});

$('input').on('keypress',function(e){
    $('input').val('');
    /*if($('.text').eq(index).text().length != 0){
        
    }*/
});

$('input').on('keyup',function(e){
    if(e.keyCode === 8){
        console.log(index);
  if($('.text').eq(index).text().length){
            $('.text').eq(index).text('');
        } else {
            if(index != 0){
            index-= 1;
$('.text').removeClass('active');
$('.text').eq(index).addClass('active');
            }
        }
    } else {
        $('.text').eq(index).text( $('input').val())
        if(index < wordLength -1){
            index++;
        } else {
            index = 3;
        }
        $('.text').removeClass('active').eq(index).addClass('active');
        
        console.log(index);
        
        
    }
});