Created
December 3, 2010 10:48
-
-
Save anderssvendal/726820 to your computer and use it in GitHub Desktop.
Make labels alternate between several form fields
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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