JavaScript Using “this” with another, outside function using this with arrow functions,using this with jquery,using

I want to use jQuery's multiple selectors and set on() on them and then using "$(this)" implement something I need. This should look like this:

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

But, I want to move $(this).something; to its own function, something like that:

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

But as you probably understand, this isn't really working. Is there some workaround this, so I could still use this in outside function?

Answer:1

Simply pass your callback function name and than use freely $(this) inside your function

function outSideFunction() {
  $(this).css({color: "red"}); // for example 
}

$(".one, .two, .three").on("someEvent", outSideFunction);

Here are some other ways:

bind

function outSideFunction() {
  $(this).css({color: "red"});
}

$(".one, .two, .three").bind("click", function(){
  outSideFunction.bind(this)();
  // Other stuff here
});

this as argument

function outSideFunction( that ) {
  $(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));
Answer:2

You can bind this to a value:

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

Example:

function outSideFunction() {
	$(this).remove();
}
$(".one, .two, .three").on("click", function () {
	 outSideFunction.bind(this)();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="one">
	Click to remove.
</div>
Answer:3

when i am trying to write code for login with linkedin i am getting error like the below. Uncaught Error: You must specify a valid JavaScript API Domain as part of this key's configuration. Below is ...

when i am trying to write code for login with linkedin i am getting error like the below. Uncaught Error: You must specify a valid JavaScript API Domain as part of this key's configuration. Below is ...

In an article on browser plugin development, they use this code: window.browser = (function () { return window.msBrowser || window.browser || window.chrome; })(); Why would they use an ...

In an article on browser plugin development, they use this code: window.browser = (function () { return window.msBrowser || window.browser || window.chrome; })(); Why would they use an ...

Implementing something based on a pathetic documentation without no info nothing. The example is just this (7F02AAF7)H => (F7AA027F)H = -139853185 Let's say even if I convert 7F02AAF7 to ...

Implementing something based on a pathetic documentation without no info nothing. The example is just this (7F02AAF7)H => (F7AA027F)H = -139853185 Let's say even if I convert 7F02AAF7 to ...

So I'm working on a list that you can scroll through by clicking on buttons. And it also needs to have a scrollbar visible and working too. But I don't know how to edit my code to get them both to ...

So I'm working on a list that you can scroll through by clicking on buttons. And it also needs to have a scrollbar visible and working too. But I don't know how to edit my code to get them both to ...

  1. scrolling buttons with tabs
  2. scrolling with buttons
  3. scrolling with buttons mouse
  4. scrolling button in html
  5. scrolling button in bootstrap
  6. scrolling button in excel
  7. scrolling in button swift
  8. horizontal scrolling with buttons
  9. parallax scrolling with buttons