I'm trying to make a dynamic input fields solution, similar to my dynamical select rows method.
The idea is simple. There always will be a extra field. So when the last field is in focus, it will clone it and append it to the wrapper.
My current code works with .focus()
, and so, is not dynamic. However, .live('focus', function () {})
doesn't. Also, "live change" or "live focusin" doesn't work.
This seems very weird problem, as it is cleared out in this question, that "live focus" should be supported since the version 1.4. However it clearly doesn't work. And most of these other questions have very localized solutions, with alternative functions.
And this is where my knowledge ends and I don't have any more imagination, for an alternative solution.
PS: If this idea is a total bust, then a change event trigger would be an alternative. However, isn't "live change" for select and/or tick inputs only?
So after all, the problem wasn't directly in the live focus function, but rather getting the correct last field. The code wasn't dynamic enough, to really re-calculate what is the currently last one. And some part of me though, that .live()
takes care of everything, but it doesn't (Don't code while tired!).
So when I changed the live functions selector manually to .dynamicalfields_wrapper input:last
, it worked fine.. So I modified my code to be more dynamic, but yet use the wrappers selector that comes to the global jQuery function.
(function ($) {
$.fn.DynamicalFieldsUpdater = function () {
return this.each(function (i, wrapper) {
$(wrapper).find('input:last').css('background', 'red').live('focus', function (i, last) {
$(wrapper).append($(this).clone().val(''));
});
});
};
})(jQuery);
$('.dynamicalfields_wrapper').DynamicalFieldsUpdater();
Works like desired. Also works with change, only that not "change"-change, but keypress.