Skip to content

Instantly share code, notes, and snippets.

@anderssvendal
Created December 3, 2010 10:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anderssvendal/726820 to your computer and use it in GitHub Desktop.
Save anderssvendal/726820 to your computer and use it in GitHub Desktop.
Make labels alternate between several form fields
(function($)
{
$.fn.extend({
alternating: function(options)
{
var options = options;
var currentElement = -1;
var timeout = 0;
// Create focus and blur events for all inputfields
options.inputs.each(function(index){
var element = $(this);
element.focus(function(){
clearTimeout(timeout);
currentElement = index;
});
element.blur(function(){
timeout = setTimeout(function(){currentElement = -1;}, 1000);
});
});
return this.each(function(){
var element = $(this);
// Remove for attribute from element
element.attr('for', '');
// Create click event
element.click(function(){
clearTimeout(timeout);
var nextElement = currentElement + 1;
if(nextElement == options.inputs.length)
{
nextElement = 0;
}
$(options.inputs[nextElement]).focus();
});
});
}
});
})(jQuery)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment