Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamic input with js/jquery

Im trying to build a dynamic input box. When I click on the numpad I want it to show on the box, and limit that box to one number. When it reaches its limit the next input would go to the next box.

I dont want the user to able to click and manually enter a value on the input box, i just want them to use the numpad. Is the input box a good solution to this?

Here's my code:

https://jsfiddle.net/Piq9117/dwxt928c/

This code puts the number that I clicked on all the boxes. How do I write it so it will go to the other box when the box already has a number?

$(function () {
    var $passBox = $('input[type="text"]');
    var $numpad = $('.numpad');

    $numpad.on('click', function () {
        var $numValue = $(this).text();
        $passBox.val($numValue);
    })
});
like image 500
ken Avatar asked Sep 12 '26 00:09

ken


2 Answers

DEMO

<div class="passcode">
    <div class="passcodeBox">
        <ul>
            <li><input type="text" readonly></li>
            <li><input type="text" readonly></li>
            <li></li>
            <li></li>
            <li></li>
        </ul>
    </div>
    <table>
        <tbody>
            <tr>
                <td class="numpad">1</td>
                <td class="numpad">2</td>
                <td class="numpad">3</td>
            </tr>
            <tr>
                <td class="numpad">4</td>
                <td class="numpad">5</td>
                <td class="numpad">6</td>
            </tr>
            <tr>
                <td class="numpad">7</td>
                <td class="numpad">8</td>
                <td class="numpad">9</td>
            </tr>
            <tr>
                <td></td>
                <td class="numpad">0</td>
            </tr>
        </tbody>
    </table>
</div>

Add attr readonly in input

like image 133
guradio Avatar answered Sep 13 '26 14:09

guradio


Make your input fields as readonly, then replace the following script, you ll get result as you expected.

$(function() {
    var $numpad = $('.numpad');
    var n=0;
    $numpad.on('click', function() {          
        var $passBox = $('input[type="text"]:eq('+n+')');        
        n=n+1;
        if(n>1)
            n=0;
        var $numValue = $(this).text();
        $passBox.val($numValue);
    });
});

DEMO

like image 28
vaishnavi Avatar answered Sep 13 '26 15:09

vaishnavi