zod zod - 1 year ago 62
Javascript Question

How can i find an element using tabindex - Javascript not jquery

I am trying to move focus to the next element after data is entered in one element.

How can i get the next element using tabindex and move focus.

i am using scanner for data entry. so one second delay on key up will tell
data entered or not. As it is scanner no browser tabbing by user .

<td class='form' align='center'> <input type='text' tabindex=1 onkeyup='moveNext(this);' id='from' name='elem1' size='5' value=''> </td>
<td class='form' align='center'><input type='text' tabindex=2 onkeyup='moveNext(this);' id='item' name='elem2' size='5' value=''> </td>
<td class='form' align='center' > <input type='text' tabindex=3 id='calc_price' size='10' name='elem3' value=''> </td>

I saw some answers. All in jquery . How can i do that in javascript

Answer Source

You'd have to get the tabindex of the current element, add 1 and then search for an element with that tabindex to set the focus to :

function moveNext(elem) {
    var tidx  = +(elem.getAttribute('tabindex')) +1,
        elems = document.getElementsByTagName('input');

    for (var i=elems.length; i--;) {
        var tidx2 = elems[i].getAttribute('tabindex');
        if (tidx2 == tidx) elems[i].focus();