Benjamin Allison
Benjamin Allison

Reputation: 2154

jQuery: looping through form based on tabindex?

So, I have an array, and I want to populate a form, but I want to loop through the form elements based on the specified tabindex, not necessarily based on the order in which they appear.

Will jQuery do this natively, or is there a way I can specify this behaviour?

FWIW, I plan on using an .each() on the input's.

Upvotes: 1

Views: 1408

Answers (1)

Ateş Göral
Ateş Göral

Reputation: 140050

You can loop through your array and select the form elements based on their tabindex attributes:

$.each(values, function (idx, value) {
    $('#myform input[tabindex="' + idx + '"]').val(value);
});

(Offset the idx variable if necessary)

Alternatively, if you want to select the input elements in one swoop:

$("#myform input").each(function () {
    var $input = $(this);
    $input.val(values[$input.attr("tabindex")]);
});

Upvotes: 1

Related Questions