jquery.mutable.js 1.12 KB
function MutarSelect(){
        
    $(".mutarselect").append("<option value='' class='mutarselect-add'>Agregar nuevo</option>");


    $(".mutarselect-add").click(function( event ){
        event.preventDefault();

        //alert($(this).parent(".mutarselect").attr("id"));
        var father = $(this).parent(".mutarselect").attr("id");
        //var positionReal = $(this).parent(".mutarselect").offset();
        //
        $(this).parent(".mutarselect").hide();
        $("<input type='text' placeholder='nuevo valor' autofocus autocomplete=off style='border:none' 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 value='" + $(this).val()  + "' class='mutarselect-rm'>" + $(this).val() + "</option>");
            $(this).prev(".mutarselect").show();
            $(this).remove();
        
        }
    });

}


$(document).ready(function(){
        MutarSelect();

});