Jquery .click or function [closed]

-1

As I get more and more accustomed, I've been left out.

<input type=button onclick="TESTE()">

<script>
function TESTE(){
...
}

To use the method in jquery

<input type=button id="teste">
<script>
$("#teste").click(function() {
...
}

I know that jQuery is a more modern method, but is it better? or can I still make a salad between the two methods?

I wondered why I recently read an article where a person compared XML and JSON, and in some cases JS was faster by working directly with XML. So sometimes we just do it because it's modern!

    
asked by anonymous 30.04.2014 / 20:50

2 answers

3

Advantage of putting direct code into element

One of the difficulties I have with jQuery is in maintaining code that I did not do.

The problem is to identify where the jQuery associated handlers are in javascript, so connecting the tips after the code is ready can prove to be an extremely tedious task.

This problem is avoided by putting the code directly into the element, even if it is to call a method, which will do everything else ... in fact this is the form I would recommend, in order to separate the rest of the script in an external file, which can take advantage of the browser cache.

Advantage of using jQuery to associate events

In terms of organization, using events association via jQuery is better because it allows the HTML (presentation) to be separated from the features (behavior). So the presentation gets decoupled from behavior .

Using jQuery is also more practical, and therefore more productive on several occasions.

  • With it you can associate events to multiple elements at the same time, using class selectors, for example.

The total separation in an external script file has another advantage, which for me, is the main one: js file cache ... but this is not an advantage of jQuery, since in the other alternative, this too is possible.

Which of the alternatives is best

Like almost everything in which there are several alternatives to choose: use common sense. It will depend on the goal. Simply align the goal with those advantages and disadvantages, as well as others you identify.

Personally I find it plausible to use both options as long as, with good judgment.

    
30.04.2014 / 20:59
0

It's always best to leave% unlinked from JavaScript , as well as HTML . In terms of functionality, the result will be the same, it's more a matter of organization and practicality.

    
30.04.2014 / 20:59