Consider following situation:
I have a jQuery Object, $wrapper = $(this);
. This happens in a init call, like this $('#unique-wrapper-id').initWrapper()'
. As you can already imagine initWrapper()
is a function in jQuery.fn
.
Now, still in the initWrapper()
call I add a function to $wrapper
like this
$wrapper.getValue = function() { /* typical wrapper function */}
and then, in a callback which is executed upon clicking on an element within the wrapper I call $wrapper.trigger('change')
.
On the other end I listen to the regular jQuery change event and here's where it doesn't work out anymore.
$('#unique-wrapper-id').change(function() {
var $wrapper = $(this);
$wrapper.getValue(); // $wrapper.getValue() is not a function
});
Ok, so somewhere in the jQuery change event process getValue()
gets lost. No problem, I'll just attach it to the DOMElement itself in the init call with
$wrapper[0].getValue = function { /* typical wrapper function */ }
This works as expected and I can execute the getValue()
method on the DOMElement behind the jQuery Object on the listening end.
However, there are two things that bug me:
- Why does
getValue()
on the jQuery object get lost during the change event process? - Why doesn't the jQuery constructor (
var $wrapper = $(this);
) copy thegetValue()
function of the DOMElement into the jQuery Object?