Calling him that way .click(function(){})
you’re wearing a shortcut for the call .on('click',function(){})
.
Already calling him without passing a function, ie this way .click()
you’re making a shortcut for the call .trigger('click')
.
This is the way to define the event via HTML <button onclick="clickTeste()">
.on(type,listener)
this method accepted beyond the event click
,change
and all other events of html
, it also accepts customized events.
Example: modal opening event of bootstrap, .on('show.bs.modal',function(){})
There is still the
<HTMLElement>.onclick=function(){}
in this you are adding an event to the element directly, using this way you will only be able to keep an event in the element and to remove the event just put the value to null
or an empty function.
<HTMLElement>.addEventListener(type,listener[, options])
already this form is very similar to jquery you can put n
events and to remove you have to call the <HTMLElement>.removeEventListener(type, listener[, useCapture])
. I believe that the jQuery.on
, jQuery.off
make use of these events.
I may be wrong but I don’t think there’s an advantage/disadvantage between .click(function(){})
and .on('click',function(){})
in this way, I think it’s just a matter of practicality and standardization.
jquery.click
, jquery.on( type [, selector ] [, data ], listener )
Does your question concern only what is done via jQuery? And is there some special reason for example 3 to have Document.ready and 2 to not have?
– bfavaretto
@bfavaretto Yes, it would be specific to jQuery. About the
document.ready
, from what I’ve been reading, there are reports that using the.on
without being in.ready
, there were cases that didn’t work. So I was even more in doubt... Until then, I think the 2nd is the most used, because it eliminates the need to set attribute in HTML, and the use of.ready
.– rbz
I marked it as duplicate because there’s a lot of question on the subject here. Your example 1 doesn’t have jQuery, so the links don’t deal with it (there are other questions on the site they deal with). The last 2 links are about the issue of Document.ready, which would be a question apart.
– bfavaretto
@bfavaretto Thanks, I’ll take a look!
– rbz