Hyperjase
Hyperjase

Reputation: 127

JQuery hidden element input issue traversing

I have a problem I just can't figure out. I have a set of DIV's which are display: none; by default:

<div id="pivot">
    <div id="leftcol">Pivot</div>
    <div id="rightcol">
        <input class="small" value="030-" disabled />
        <input class="input" id="pi" maxlength="6" />
        <span id="pit"></span>
    </div>
</div>
<div id="pos1">
    <div id="leftcol">Position 1</div>
    <div id="rightcol">
        <input class="small" value="031-" disabled />
        <input class="input" id="p1" maxlength="6" />
        <span id="p1t"></span>
    </div>
</div>
<div id="pos2">
    <div id="leftcol">Position 2</div>
    <div id="rightcol">
        <input class="small" value="031-" disabled />
        <input class="input" id="p2" maxlength="6" />
        <span id="p2t"></span>
    </div>
</div>
<div id="pos3">
    <div id="leftcol">Position 3</div>
    <div id="rightcol">
        <input class="small" value="031-" disabled />
        <input class="input" id="p3" maxlength="6" />
        <span id="p3t"></span>
    </div>
</div>
<div id="pos4">
    <div id="leftcol">Position 4</div>
    <div id="rightcol">
        <input class="small" value="031-" disabled />
        <input class="input" id="p4" maxlength="6" />
        <span id="p4t"></span>
    </div>
</div>
<div id="pos5">
    <div id="leftcol">Position 5</div>
    <div id="rightcol">
        <input class="small" value="031-" disabled />
        <input class="input" id="p5" maxlength="6" />
        <span id="p5t"></span>
    </div>
</div>
<div id="pos6">
    <div id="leftcol">Position 6</div>
    <div id="rightcol">
        <input class="small" value="031-" disabled />
        <input class="input" id="p6" maxlength="6" />
        <span id="p6t"></span>
    </div>
</div>

Then this code unhides the relevant section:

$.ajax({        
    url: "getSerial.asp",
    data: { term: term },
    dataType: "json",
    success: function(data) {
        $('option:selected').append('-' + data[0].sn)
        $('#prefix').attr('disabled', true)
        var code = data[0].partno.substring(6, 12);
        len = code.length;
        $('#pivot').show(0);
        for (var i = 0; i < len; i++) {
            if (code.substr(i, 1) != "-") {
                $('#pos' + code.substr(i, 1)).show(0);
            }                   
        }
        $('div').find('input:enabled').first().focus();
    }       
}); 

So that works perfectly fine. Then what I'm trying to do is as you type in 6 characters, it automatically jumps down to the next visible input (class="input"). My code for that is very un-glamorous and works ONLY with the next logical div (pos1, pos2 etc). The combination may be pos1,pos3,pos3 so missing out number 2 (as it is still hidden).

$(this).parent('div').parent('div').next('div').find('.input:visible').focus()

This doesn't work for the above combination. How do I traverse successfully only to the next visible .input class?

Upvotes: 0

Views: 71

Answers (1)

Barmar
Barmar

Reputation: 782508

Use :has() in the selector to match a DIV that contains a visible input.

$(this).parent('div').parent('div').nextAll('div:has(.input:visible)').first().find('.input:visible').focus();

Upvotes: 2

Related Questions