jquery - Pushing variable to an existing function with javascript -
so, lets have function this
(function($){ $.fn.test = function(opts){ var _object = $(this), _opts = $.extend({}, $.fn.test.defaults, opts), _callback = _opts.objects[1].callback /* code here */ _callback() /* calling callback */ } $.fn.test.defaults = { /* not important */ } })
that's how initialise function
$('.element').test({ option: 0, /* not important */ variable: 1, /* not important */ objects: [ { "object" : ".element2", "callback" : false }, { "object" : ".element3", /* object № 2 */ "callback" : function(){ /* >>> {this part} <<< */ console.log (this) } }, ]
so on {this part} returns data of object №2, callback function run on _object element $.fn.test output data of $('.element'). can't make hardcoded, , can't put instead console.log (this)
console.log ($('.element'))
- i'm trying make more dynamic function. need replace this _object element @ moment when it's calling callback right here _callback() /* calling callback */
. can't use replace it's not string, had idea convert function string, change need , convert function - sounds inappropriate solution.
do have ideas in mind? can suggest?
thank in advance.
i'm not sure if understand correctly if want $.fn.test scope can try this:
$.fn.test = function(opts){ var _object = $(this), _opts = $.extend({}, $.fn.test.defaults, opts), _callback = _opts.objects[1].callback; _callback(this); }
and
$('.element').test({ option: 0, variable: 1, objects: [ { "object": ".element2", "callback": false }, { "object": ".element3", "callback" : function(parentscope){ console.log(parentscope); } }, ] });
Comments
Post a Comment