What is the correct way to assign a function to an HTML event

Asked

Viewed 42 times

3

In programming we know that it is possible to get the same result by following different paths, however, there are also good and bad practices.

There are websites and developers that use different ways to call a javascript function, for example:

<button onClick="funcao()" id="chamaFuncao">

and to call the same function with jQuery would be:

$("#chamaFuncao").click(function(){});

We could also call it with pure javascript, but for my doubt these 2 examples are enough.

What is the best or correct way to call a function?

In programming there are good and bad practices, in the examples above it will be that one of the methods is a good practice and the other will be a bad practice?

  • Actually the calls are different, with different functions. It’s okay to associate an event in html. It might even be clearer. But sometimes you don’t want this association from the start. Maybe you just want to associate from some point, and this needs to be done by Jquery.

  • I found interesting the question, I am more backend so I have no property to answer but I consider calling the function by jQuery a good practice to "decouple" the logical part of HTML

  • 3

    I would say that this question is duplicated: http://answall.com/q/25821/129

  • 1

    I understand the difference. But let’s say that my need is to display an Alert with an x message, for this I can create the Alert from jQuery or from the onClick event. The question is: What would be the right way(good practice) and how to distinguish when to use one and when to use the other.

  • 1

    @Sergio This link answers my question! Thanks.

  • 1

    I have already answered a question similar to this but in Stack Overflow in English

Show 1 more comment
No answers

Browser other questions tagged

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