javascript - Using "this" with another, outside function -


i want use jquery's multiple selectors , set on() on them , using "$(this)" implement need. should this:

$(".one, .two, .three").on("someevent", function () {   $(this).something; }); 

but, want move $(this).something; own function, that:

function outsidefunction() {   $(this).something; } $(".one, .two, .three").on("someevent", function () {   outsidefunction(); }); 

but understand, isn't working. there workaround this, still use this in outside function?

simply pass callback function name , use freely $(this) inside function

function outsidefunction() {   $(this).css({color: "red"}); // example  }  $(".one, .two, .three").on("someevent", outsidefunction); 

here other ways:

bind

function outsidefunction() {   $(this).css({color: "red"}); }  $(".one, .two, .three").bind("click", function(){   outsidefunction.bind(this)();   // other stuff here }); 

this argument

function outsidefunction( ) {   $(that).css({color: "red"}); }  $(".one, .two, .three").on("click", function() {   outsidefunction(this);   // other stuff here }); 

$.proxy

function outsidefunction() {   $(this).css({color: "red"}); }  $(".one, .two, .three").on("click", $.proxy(outsidefunction, this.selector)); 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

thorough guide for profiling racket code -