Blame view

js/jquery.mutable.js 1.66 KB
35d5ac987   Guillermo Moguel   actualizado a plu...
1
2
3
4
5
6
7
8
  (function ( $ ) {
      $.fn.mutarSelect = function( options ){
  
  
          // This is the easiest way to have default options.
          var settings = $.extend({
          valueselector:"Agregar nuevo",
          }, options );
92059f3e3   Guillermo Moguel   Finalizado parcial
9
10
          var father = $(this).attr("id");
          $(this).append("<option value='' class='mutarselect-add'>" + settings.valueselector + "</option>");
35d5ac987   Guillermo Moguel   actualizado a plu...
11
      
92059f3e3   Guillermo Moguel   Finalizado parcial
12
          $("#" + father + " .mutarselect-add").click(function( event ){
35d5ac987   Guillermo Moguel   actualizado a plu...
13
                  event.preventDefault();
92059f3e3   Guillermo Moguel   Finalizado parcial
14

35d5ac987   Guillermo Moguel   actualizado a plu...
15
                  $("#" + father + " .mutarselect-rm" ).remove();
92059f3e3   Guillermo Moguel   Finalizado parcial
16
17
18
                  $(this).parent("#" + father).hide();
                                 
                  $("<input type='text' placeholder='nuevo valor' autofocus autocomplete=off' class='mutarselect-input " + father +"' >").insertAfter( $(this).parent(".mutarselect") );
35d5ac987   Guillermo Moguel   actualizado a plu...
19
          });    
35d5ac987   Guillermo Moguel   actualizado a plu...
20
21
22
23
24
25
26
27
28
29
30
             
          $("form").on("keypress", ".mutarselect-input",function( event ){
              if ( event.keyCode == '13' ){
                  event.preventDefault();
  
                  $(this).prev(".mutarselect").prepend("<option selected=\"selected\" value='" + $(this).val()  + "' class='mutarselect-rm'>" + $(this).val() + "</option>");
                  $(this).prev(".mutarselect").show();
                  $(this).remove();
                  
              }
          });
69118e51a   Guillermo Moguel   Creado plugin con...
31
32
33
34
35
36
37
  
        $("form").on("focusout", ".mutarselect-input",function( event ){
                  $(this).prev(".mutarselect").prepend("<option selected=\"selected\" value='" + $(this).val()  + "' class='mutarselect-rm'>" + $(this).val() + "</option>");
                  $(this).prev(".mutarselect").show();
                  $(this).remove();
  
          });
35d5ac987   Guillermo Moguel   actualizado a plu...
38
39
40
41
          //return this;
  
      };
  }( jQuery ));
a8763fd27   Guillermo Moguel   Inicio de desarrollo
42