Get click (this) element in method

Asked

Viewed 104 times

0

I’m refactoring a SPA using in practice the MVC with classes and etc...

I made a method that is called when I click on an element <i> and would like to obtain this element to subsequently delete a relative of his (a <tr> two levels above).

I’m calling this event in HTML with attr onclick="CalculaController.removeLinha()"

But if I ask to take the this it returns me the controller class and not the element clicked, I did some tests with event type .on('click', function(){return this}); works but does not stay 100% this with some errors...

  • 1

    Post your code so we can help...

  • Solved! in the attr I was calling the metodo with this, but in the metodo I put as parametro this tbm, and this within the class always is the class

1 answer

2


To get your element in the function you can send it as a parameter to it. Ex:

<i onclick="FuncaoTeste(this);">Teste</i>

var FuncaoTeste = function(element)
{
    console.log(element);
}

If you’re interested, take a closer look at how this.

  • My problem was that I set this as a parameter in the method.. I just changed it to element, thank you!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.