0

I have a button with onclick method which calls function.

What is not clear to me, why is this inside function seen as Object? Isn't this suppose to be object calling method? In this case it is the button?

I pass only one parameter as this to the method. And this parameter correctly shows button (named parameter element inside function).

Why is this inside function not showing button inside DOM which called the method?

var test = (function() {
  var test1 = {
    nameT: 'test'
  };

  methodTesting(test1);

  function methodTesting(element) {
    debugger;
  }
  return {
    methodTesting: methodTesting
  }
});
<button onclick="test.methodTesting(this);" data-itest=1 data-ctest2='miran' data-ct='feri'>TEst</button>

enter image description here

Mr. Polywhirl
  • 42,981
  • 12
  • 84
  • 132
FrenkyB
  • 6,625
  • 14
  • 67
  • 114

1 Answers1

1

It's not a reference to the element because you're assigning it via the onclick attribute.

Use addEventListener instead or get a reference to the element via the currentTarget property of the Event.

André Dion
  • 21,269
  • 7
  • 56
  • 60
  • If I understand this correctly onclick is also an object and that object is calling object in this case? – FrenkyB Apr 20 '17 at 16:48