javascript - Why does keyup not work -
i'm trying input text fields focus next in line when keyup.
jquery code:
for (var counter = 0; counter < 4; counter++) { $("<input>", { "class":"anbox", size: 1, maxlength: 1 }).appendto('#answerline_' + counter); }; $(".anbox").keyup(function () { if (this.value.length == this.maxlength) { $(this).next('.anbox').focus(); } }); here a: jsfiddle
your usage of .next() incorrect. .next() function looks next sibling of element(s) in current set, optionally checking next sibling matches selector provided.
your <input> elements in different <span> elements, though, aren't siblings, , therefore can't matched using .next().
what you'll need go current <input> ($(this)), parent (.parent()), next <span> element (.next() or .next('span')), find .anbox element inside of (.find('.anbox')), focus (.focus()). looks this:
$(this).parent().next().find('.anbox').focus(); take @ updated jsfiddle demo.
Comments
Post a Comment