Commit 69118e51a9630a82e1f6807f4cc5173158f0e9d7
1 parent
92059f3e3d
Exists in
master
Creado plugin con opcion de mensaje
Showing
1 changed file
with
8 additions
and
0 deletions
Show diff stats
js/jquery.mutable.js
1 | (function ( $ ) { | 1 | (function ( $ ) { |
2 | $.fn.mutarSelect = function( options ){ | 2 | $.fn.mutarSelect = function( options ){ |
3 | 3 | ||
4 | 4 | ||
5 | // This is the easiest way to have default options. | 5 | // This is the easiest way to have default options. |
6 | var settings = $.extend({ | 6 | var settings = $.extend({ |
7 | valueselector:"Agregar nuevo", | 7 | valueselector:"Agregar nuevo", |
8 | }, options ); | 8 | }, options ); |
9 | 9 | ||
10 | var father = $(this).attr("id"); | 10 | var father = $(this).attr("id"); |
11 | $(this).append("<option value='' class='mutarselect-add'>" + settings.valueselector + "</option>"); | 11 | $(this).append("<option value='' class='mutarselect-add'>" + settings.valueselector + "</option>"); |
12 | 12 | ||
13 | $("#" + father + " .mutarselect-add").click(function( event ){ | 13 | $("#" + father + " .mutarselect-add").click(function( event ){ |
14 | event.preventDefault(); | 14 | event.preventDefault(); |
15 | 15 | ||
16 | $("#" + father + " .mutarselect-rm" ).remove(); | 16 | $("#" + father + " .mutarselect-rm" ).remove(); |
17 | $(this).parent("#" + father).hide(); | 17 | $(this).parent("#" + father).hide(); |
18 | 18 | ||
19 | $("<input type='text' placeholder='nuevo valor' autofocus autocomplete=off' class='mutarselect-input " + father +"' >").insertAfter( $(this).parent(".mutarselect") ); | 19 | $("<input type='text' placeholder='nuevo valor' autofocus autocomplete=off' class='mutarselect-input " + father +"' >").insertAfter( $(this).parent(".mutarselect") ); |
20 | 20 | ||
21 | }); | 21 | }); |
22 | 22 | ||
23 | $("form").on("keypress", ".mutarselect-input",function( event ){ | 23 | $("form").on("keypress", ".mutarselect-input",function( event ){ |
24 | if ( event.keyCode == '13' ){ | 24 | if ( event.keyCode == '13' ){ |
25 | event.preventDefault(); | 25 | event.preventDefault(); |
26 | 26 | ||
27 | $(this).prev(".mutarselect").prepend("<option selected=\"selected\" value='" + $(this).val() + "' class='mutarselect-rm'>" + $(this).val() + "</option>"); | 27 | $(this).prev(".mutarselect").prepend("<option selected=\"selected\" value='" + $(this).val() + "' class='mutarselect-rm'>" + $(this).val() + "</option>"); |
28 | $(this).prev(".mutarselect").show(); | 28 | $(this).prev(".mutarselect").show(); |
29 | $(this).remove(); | 29 | $(this).remove(); |
30 | 30 | ||
31 | } | 31 | } |
32 | }); | 32 | }); |
33 | 33 | ||
34 | |||
35 | $("form").on("focusout", ".mutarselect-input",function( event ){ | ||
36 | $(this).prev(".mutarselect").prepend("<option selected=\"selected\" value='" + $(this).val() + "' class='mutarselect-rm'>" + $(this).val() + "</option>"); | ||
37 | $(this).prev(".mutarselect").show(); | ||
38 | $(this).remove(); | ||
39 | |||
40 | }); | ||
41 | |||
34 | //return this; | 42 | //return this; |
35 | 43 | ||
36 | }; | 44 | }; |
37 | }( jQuery )); | 45 | }( jQuery )); |
38 | 46 | ||
39 | 47 |