-1

I have

function Outer(x, y){
     var X = x;
     this.x = x;

      $('#div1').find('input[type=checkbox]').each(function(index, values){
           $(values).text(x); //x is undefined
           $(values).text(X); //X is undefined
      });
}

How to pass the values to the inner functions?

user544079
  • 16,109
  • 42
  • 115
  • 171
  • 3
    Both capital and lower case x will work. – Felix Kling Oct 08 '15 at 21:51
  • 5
    Setting the `.text()` of an `` element makes no sense. You have to set the value via `.val()`. – Pointy Oct 08 '15 at 21:51
  • Show us how you call `Outer`, please. Very likely, [`this`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/this)`.x` is a grave mistake. – Bergi Oct 08 '15 at 21:52
  • LOL. I love it. Somebody down-voted the question just because they didn't understand it. "Aaaaargh. Experiencing confusion. Must smash." – cutmancometh Oct 08 '15 at 21:55

2 Answers2

3

First: all JavaScript functions have access to variables defined on their parent scope. see here for more.

Second: 'this' does not refer to 'itself' as you are using it, it refers to the call site of the function in question.

So in your case, all you need to do is

function Outer(x, y) {
  $('#div1').find('input[type=checkbox]').each(function(index, values){
       $(values).text(x);
  });
}

and then call Outer with values of x and y

Outer(2, 3);
Community
  • 1
  • 1
Jonah Williams
  • 20,499
  • 6
  • 65
  • 53
1

Use simply x:

function Outer(x, y){
  $('#div1').find('input[type=checkbox]').each(function(index, values){
       $(values).text(x);
  });
}

Because the inner function has access to the scope of the outer function.

If x is undefined, then you are passing undefined to Outer.

Tim
  • 5,521
  • 8
  • 36
  • 69