Slines

habtmtags

Sep 29th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
jQuery 2.12 KB | None | 0 0
  1. ( function($) {
  2.         $.fn.habtmTags = function(options) {
  3.  
  4.             var settings = $.extend({
  5.                 add : null,
  6.  
  7.             }, options);
  8.  
  9.             var str = "";
  10.             var selector = this.selector; //#Tags
  11.             var selected = "";
  12.             var input = '<input type="text" id="typeahead" />';
  13.             var tagsContainer = '<div id="str"></div>';
  14.  
  15.             // get values from multiple select list and construct tags elements
  16.             $(selector + "> option").each(function() {
  17.                 if ($(this).prop('selected')) {
  18.                     str += '<span class="label label-info">' + $(this).text() + ' <a href="#' + $(this).val() + '" class="remove-tag" id="' + $(this).val() + '">x</a></span>' + ' ';
  19.                     selected += $(this).val();
  20.                 }
  21.             });
  22.            
  23.  
  24.             // create tags elements
  25.             //$(this.selector).css('display','none').after(tagsContainer);
  26.             $(selector).after(tagsContainer);
  27.             $("#str").html(str + ' ' + input);
  28.            
  29.             // remove Tags and unselect current list option
  30.             /**$(".remove-tag").on('click',function(event) {
  31.                 var id = event.target.id;
  32.                 alert(id);
  33.                 $(selector + "> option[value$='" + id + "']").removeAttr("selected");
  34.                 //$(selector + "> span[id$='"+id+"']").remove();
  35.                 $(this).parent().remove();
  36.             });*/
  37.  
  38.             $("#str").delegate(".remove-tag", "click", function (event){
  39.                 var id = event.target.id;
  40.                 //alert(id);
  41.                 $(selector + "> option[value$='" + id + "']").removeAttr("selected");
  42.                 $(selector + "> span[id$='"+id+"']").remove();
  43.                 $(this).parent().remove();  
  44.             });
  45.  
  46.             // add new tag value in database and append select list and create new span element
  47.  
  48.             $("#typehead").keypress(function(event) {
  49.                 if (event.which == 13) {
  50.                     event.preventDefault();
  51.                     $.post('../../oznake/dodaj',{ name: $("#typeahead").val(), user_id: "1" })
  52.                         .done(function(data){
  53.                             $( selector ).append( '<option value="'+data+'" selected="selected">'+$("#typeahead").val()+'</option>' );
  54.                             $("#typeahead").before('<span class="label label-info">' + $("#typeahead").val() + ' <a href="#' + data + '" class="remove-tag" id="' + data + '">x</a></span>' + ' ');
  55.                             $("#typeahead").val('');
  56.                         });
  57.                    
  58.                 }
  59.                
  60.             });
  61.  
  62.  
  63.         };
  64.     }(jQuery));
Advertisement
Add Comment
Please, Sign In to add comment