/** * @version 0.0.1 * @author Martin Mende * @license Attribution-NonCommercial 3.0 Unported (CC BY-NC 3.0) * @license For comercial use please contact me: martin.mende(at)aristech.de * @requires jQuery 1.9+ * @class editableSelect * @memberOf jQuery.fn */ (function(e){function t(t,n){var r=e("
  • "+t+"
  • ").appendTo(n.children("ol"));var i=n.children("input");r.css({padding:"3px",textAlign:"left",cursor:"pointer"}).hover(function(){r.css({backgroundColor:"#eee"})},function(){r.css({backgroundColor:"#fff"})});r.click(function(){i.val(r.text());i.trigger("change")})}e.fn.editableSelect=function(){var n;this.each(function(){var r=e(this);if(r[0].tagName.toUpperCase()==="SELECT"){r.wrap(e("
    "));var i=r.parent();i.css({display:"inline-block"});var s=e("").insertBefore(r);var o=r.attr("id");r.removeAttr("id");s.attr({alt:r.attr("alt"),title:r.attr("title"),"class":r.attr("class"),name:r.attr("name"),disabled:r.attr("disabled"),tabindex:r.attr("tabindex"),id:o});var u=15;s.css({width:r.width()-u,height:r.height(),fontFamily:r.css("fontFamily"),fontSize:r.css("fontSize"),background:r.css("background"),paddingRight:u});s.val(r.val());var a=e("
    ").css({height:0,width:0,borderLeft:"5px solid transparent",borderRight:"5px solid transparent",borderTop:"7px solid #999",position:"relative",top:-(s.height()/2)-5,left:s.width()+u-10,marginBottom:"-7px",pointerEvents:"none"}).insertAfter(s);var f=e("
      ").css({display:"none",listStyleType:"none",width:s.outerWidth()-2,padding:0,margin:0,border:"solid 1px #ccc",fontFamily:s.css("fontFamily"),fontSize:s.css("fontSize"),background:"#fff",position:"absolute",zIndex:1e6}).insertAfter(a);r.children().each(function(n,r){t(e(r).text(),i)});s.focus(function(){f.fadeIn(100)}).blur(function(){f.fadeOut(100)}).keyup(function(e){if(e.which==13)s.trigger("blur")});r.css({visibility:"hidden",display:"none"});n=s}else{return false}});n.addOption=function(e){t(e,n.parent())};n.removeOption=function(t){switch(typeof t){case"number":n.parent().children("ol").children(":nth("+t+")").remove();break;case"string":n.parent().children("ol").children().each(function(n,r){if(e(r).text()==t){e(r).remove()}});break}};n.restoreSelect=function(){var e=n.parent().children("select");var t=n.attr("id");n.parent().before(e);n.parent().remove();e.css({visibility:"visible",display:"inline-block"});e.attr({id:t})};return n}})(jQuery)