{"id":37810,"date":"2016-02-02T20:47:42","date_gmt":"2016-02-03T02:47:42","guid":{"rendered":"http:\/\/develop.ibox.mx\/?p=162"},"modified":"2016-08-02T08:41:30","modified_gmt":"2016-08-02T13:41:30","slug":"eventos-personalizados-en-jquery","status":"publish","type":"post","link":"https:\/\/webirix.com\/en\/eventos-personalizados-en-jquery\/","title":{"rendered":"Custom events in jQuery"},"content":{"rendered":"<p>All of us who have handled javascript or jquery know what events are, but if you are a newbie here we will put you what an event is:<br \/>\nAn event in the world of programming is known to any action that the user performs with the system or in our case on the website, such as the most common ones that are presented below according to their respective language.<!--more--><\/p>\n<figure id=\"attachment_165\" aria-describedby=\"caption-attachment-165\" style=\"width: 290px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" class=\"size-medium wp-image-165\" src=\"http:\/\/develop.ibox.mx\/wp-content\/uploads\/2016\/02\/Captura-300x100.png\" alt=\"Lista de eventos\" width=\"300\" height=\"100\" \/><figcaption id=\"caption-attachment-165\" class=\"wp-caption-text\">List of most common events<\/figcaption><\/figure>\n<p>Its method of use as many have known it is in jQuery as shown below:<\/p>\n<pre><code class=\"javascript\">\r\n$ (&#039;. Btn&#039;). Click (function (event) {event.preventDefault (); alert (&#039;Hello world.&#039;);});\r\n<\/code>\r\n<\/pre>\n<p>En este ejemplo estamos asignando un m\u00e9todo click a cualquier elemento que tenga la clase btn, despu\u00e9s detenemos el evento por defecto que tenga el elemento y finalmente mostramos un mensaje de con la leyenda &#8220;Hola mundo&#8221;.<\/p>\n<p>This was a bit of basic theory of event handling in jQuery, but if it happened that we wanted the same action in another module, we would have to copy and paste in a different selector.<br \/>\nBut what if I told you that there is a way to create your own event methods. We will see this below:<\/p>\n<p>First create an html called index.html and do not forget to incorporate the jQuery library then we place the code to add the js<\/p>\n<p>&lt;script src=&#8217;https:\/\/ajax.googleapis.com\/ajax\/libs\/jquery\/2.2.0\/jquery.min.js&#8217;&gt;&lt;\/script&gt;<\/p>\n<p>We will also need to create a js file called global_function.js which we must link with our html as follows<\/p>\n<p>&lt;script src=&#8217;js\/global_function.js&#8217;&gt;&lt;\/script&gt;<\/p>\n<p><em>Note: It is advisable to place the js at the end of the body to speed up the loading\u00a0<\/em><\/p>\n<p>Now to continue we must place a method either ready or $ (function ()) to start with the script, I will use the following to start as a plugin<\/p>\n<pre><code class=\"javascript\">\r\n$ (function () {#here we will put our code});\r\n<\/code>\r\n<\/pre>\n<p>Ahora deberemos continuar con lo siguiente que es crear nuestro m\u00e9todo de evento personalizado , lo haremos con la funci\u00f3n de jquery &#8220;$.fn&#8221; que es una propiedad de javascript para crear funciones que afectaran a uno o mas selectores. Mi selector se llamara &#8220;mostrar_alerta&#8221;<\/p>\n<p>&nbsp;<\/p>\n<pre>     <code class=\"javascript\">\r\n         $ (function () {$.fn.show_alert = function () {alert (&#039;Hello world. This is from my created event&#039;);}})\r\n     <\/code>\r\n<\/pre>\n<p>Now to call our custom event we will have to change our initial code a bit with the following way<\/p>\n<p>&nbsp;<\/p>\n<pre><code class=\"javascript\">\r\n$ (&#039;. Btn&#039;). Click (function (event) {event.preventDefault (); \/\/ alert (&#039;Hello world.&#039;); $ (this) .show_alert ();});\r\n<\/code>\r\n<\/pre>\n<p>And if we execute it, we will see the response of our new message in any button.<\/p>\n<p>Also, custom events can incorporate methods, for example AJAX, to make a request to another file, for example:<\/p>\n<p>&nbsp;<\/p>\n<pre><code class=\"javascript\">\r\n$ (&#039;. Btn&#039;). Click (function (event) {event.preventDefault (); \/\/ alert (&#039;Hello world.&#039;); Var url = &#039;ajax \/ clients \/ create&#039;; var form = $ (&#039;#my -form &amp; &#039;). serialize (); var result = $ (&#039;. message &amp; &#039;); $ (this) .request (url, form, result);}); $.fn.request = function (url, form, result) {$.ajax ({type: &#039;POST&#039;, url: url, data: form, success: function (respond) {result.html (respond);}} ); }\r\n<\/code>\r\n<\/pre>\n<p>&nbsp;<\/p>\n<h2>Advantage<\/h2>\n<ul>\n<li>Reduction of repeated methods<\/li>\n<li>More ordered code<\/li>\n<li>Recycle code in different projects<\/li>\n<\/ul>\n<h2>References of interest<\/h2>\n<p><a title=\"Booksweb\" href=\"http:\/\/librosweb.es\/libro\/fundamentos_jquery\/capitulo_11.html\" target=\"_blank\">Chapter 11<\/a><\/p>\n<p><a href=\"https:\/\/learn.jquery.com\/plugins\/basic-plugin-creation\/\" target=\"_blank\">Basic plugin creation<\/a><\/p>\n<p><a href=\"http:\/\/www.iteramos.com\/pregunta\/3516\/los-eventos-personalizados-en-jquery\">Custom events in jquery<\/a><\/p>","protected":false},"excerpt":{"rendered":"<p>All of us who have handled javascript or jquery know what events are, but if you are a newbie here we will put you what an event is: An event in the world of programming is known to any action that the user performs with the system or in our case on the website as ...<\/p>","protected":false},"author":21,"featured_media":37825,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2166,2155,2175],"tags":[],"class_list":["post-37810","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-desarrollo","category-jquery","category-programacion"],"jetpack_featured_media_url":"https:\/\/webirix.com\/wp-content\/uploads\/2016\/02\/levitando-plena-calle-quieres-saber-como-hace-L-7c8Nnb.jpeg","_links":{"self":[{"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/posts\/37810","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/comments?post=37810"}],"version-history":[{"count":1,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/posts\/37810\/revisions"}],"predecessor-version":[{"id":37826,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/posts\/37810\/revisions\/37826"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/media\/37825"}],"wp:attachment":[{"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/media?parent=37810"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/categories?post=37810"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/webirix.com\/en\/wp-json\/wp\/v2\/tags?post=37810"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}