simulate tabindex

by photo_tom

HTML

<form>
          <input class="focusable" type="text" value="lol"/>
          <input class="focusable" type="text" value="lol" />
          <input class="focusable" type="text" value="lol" />
          <input class="focusable" type="text" value="lol" />
          <input class="focusable" type="text" value="lol" />
          <input class="focusable" type="text" value="lol" />                 
        </form> 

        <input type="button" id="trigger_button" value="  lol  " />

JavaScript

var focusable = $('.focusable'),
    last_element_index = focusable.length - 1,
    current_index;

focusable.each(function(i) {
    $(this).click(function() {
        current_index = i;
    })
});

$('#trigger_button').click(function() {
    current_index = (should_reset_current_index()) ? 0 : current_index + 1;
    focusable[current_index].focus();
});

function should_reset_current_index() {
    return (typeof(current_index) === 'undefined') || (current_index == last_element_index)
};