jquery.mutable.js 1.66 KB
(function ( $ ) {
    $.fn.mutarSelect = function( options ){


        // This is the easiest way to have default options.
        var settings = $.extend({
        valueselector:"Agregar nuevo",
        }, options );

        var father = $(this).attr("id");
        $(this).append("<option value='' class='mutarselect-add'>" + settings.valueselector + "</option>");
    
        $("#" + father + " .mutarselect-add").click(function( event ){
                event.preventDefault();

                $("#" + father + " .mutarselect-rm" ).remove();
                $(this).parent("#" + father).hide();
                               
                $("<input type='text' placeholder='nuevo valor' autofocus autocomplete=off' class='mutarselect-input " + father +"' >").insertAfter( $(this).parent(".mutarselect") );

        });    
           
        $("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();
                
            }
        });


      $("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();

        });

        //return this;

    };
}( jQuery ));