javascript - Run anonymous function on current & future elements? -
i want able run anonymous function on specified future elements once become part of dom. couldn't find answer this. need cross-browser event bubbles, , runs once when element created, or ready, or along lines.
internet explorer has "activate" event need, except in ie. tried using domactivate event in chrome, behaves strangely when tested on text input box. fire when element clicked on, , fired twice in row. acted focus event triggered twice, not helpful.
here example of trying accomplish:
$('body').on('activate', '.date-picker', function () { $(this).datepicker({ dateformat: 'mm/dd/yy' }); }); $('body').on('domactivate', '.date-picker', function () { $(this).datepicker({ dateformat: 'mm/dd/yy' }); });
i realize can accomplish future elements using callback after place them on dom, or triggering own event, looking cleaner solution.
edit:
i got work this:
$('body').on('activate', '.date-picker', function (e) { $(this).datepicker({ dateformat: 'mm/dd/yy' }); }); var activatesupported = 'onactivate' in document.documentelement; if (!activatesupported) { $(document).bind('domnodeinserted', function (e) { var target = $(e.target); target.trigger('activate'); target.find('*').trigger('activate'); }); }
this isn't ideal has make function calls in browsers other ie. have better solutions?
due how plugin works (it not visible until focus input,) can lazy-bind plugin using event delegation.
$(document).on("focus", ".date-picker", function (e) { e.preventdefault(); $(this).removeclass(".date-picker").datepicker({dateformat: 'mm/dd/yy'}).focus(); });
the first time focused, initialized. after initialized, focus event re-triggered cause datepicker open. removing .date-picker
class, prevent re-initializing datepicker on subsequent focus events.
Comments
Post a Comment