Date: 2014apr11
Language: javaScript
Library: jQuery
Keywords: call, apply, bind
Q. jQuery: How do I pass along "this" from one function to another?
I have a function that expects "this" to be set:
function somethingChanged() {
let id = $(this).attr('id');
let val = $(this).val();
console.log(id + ' changed to ' + val);
}
If is invoked by jQuery but how can I call it with "this" set?
A. If your function has "this" you can call the other function using javaScript's call:
function myOtherFunction() {
somethingChanged.call(this);
}