If the containers are Div01 , etc. can have sortable identifiers, as in your example, you can do this
jquery solution
var groups = $('div[id^="Div"]').sort(function(a,b){ return (a.id > b.id) ? 1 : -1; }); groups.find(':input').each(function(idx){ $(this).prop('tabindex', idx+1); });
Demo at http://jsfiddle.net/gaby/sNekS/
Alternatively (and most likely more correctly), you can simply rearrange your divs so that they are correctly sorted in the source file and still appear in the left and right groups when rendering (using float:left on the inner divs) and generally not use scripts.
Demo at http://jsfiddle.net/gaby/sNekS/1/
Vanilla Javascript solution (after adding the group class to Div## elements and the input class to input / select elements / etc)
var gnodes = document.getElementsByClassName('group'); // find elements with group class - non-sortable var groups = []; // create empty array to hold groups - sortable for (var i = 0, l = gnodes.length; i<l; i++){ // place elements in array so we can sort it groups.push( gnodes[i] ); } groups.sort(function(a,b){ // sort the array based on id return (a.id > b.id) ? 1 : -1; }); var counter = 1; // incremental number to define the tabindex for (var i = 0, l = groups.length; i<l; i++){ var group = groups[i], elements = group.getElementsByClassName('input'); // find all input elements of this group (must add class to all of them) for (var e = 0, len = elements.length; e < len; e++){ elements[e].setAttribute('tabindex',counter++); // set tabindex } }
Demo at http://jsfiddle.net/gaby/sNekS/3/
Gaby aka G. petrioli
source share