Dave's Brain

Browse - Programming Tips - How do I pass along "this" from one function to another?

Date: 2014apr11
Language: javaScript
Library: jQuery

Q.  How do I pass along "this" from one function to another?

I have a function that expects "this" to be set:

function somethingChanged() { var id = $(this).attr('id'); var val = $(this).val(); console.log(id + ' changed to ' + val); }
It 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); }

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.