function Tag(element, config) {

  $.extend(options, config)

  // Private variables
  var el = $(element);
  var select = el.find('select');
  var html;

  // base setup
  function init() {
    renderTags();

    select.on('change', function() {
      renderTags();
    });

    // delegate the tag close click to the container
    el.on('click', '[data-tag]', function(event) {
      event.preventDefault();
      remove(event.target);
    });
  }

  function updateArray() {
    // @todo @refactor - weirdly, mapping returns on object rather than an array
    // of objects, so doing this like this temporarily
    var arr = [];
    select.find('option').each(function(i, v) {
      arr.push({
        value: v.value,
        label: v.text,
        selected: v.selected,
        index: i
      })
    });

    return arr;
  }

  //
  function renderTags() {
    if (html) {
      html.remove();
    }
    var arr = updateArray();
    var items = arr.filter(function(v) {
      return v.selected;
    });
    html = $(mustache.render(options.template, {items: items}));
    select.after(html);
  }

  // accepts element and toggles the index of the option
  // element to de-select. Removes tag
  function remove(element) {
    var index = element.getAttribute('data-index');
    select[0].options[index].selected = false;
    element.parentNode.parentNode.removeChild(element.parentNode);
  }

  /**
   * Expose public methods
   */
  var self = {
    init: init
  };

  return self;

}
/**
 * Helper function to extend a <select multiple
 * so as to create tags for assigning / priorities etc
 */
Helper function to extend a <select multiple
so as to create tags for assigning / priorities etc
Helper function to extend a <select multiple so as to create tags for assigning / priorities etc
function Tag(element, config) {
  $.extend(options, config)
  var el = $(element);
  var select = el.find('select');
  var html;
  function init() {
    renderTags();
    select.on('change', function() {
      renderTags();
    });
    el.on('click', '[data-tag]', function(event) {
      event.preventDefault();
      remove(event.target);
    });
  }
  function updateArray() {
    var arr = [];
    select.find('option').each(function(i, v) {
      arr.push({
        value: v.value,
        label: v.text,
        selected: v.selected,
        index: i
      })
    });
    return arr;
  }
  function renderTags() {
    if (html) {
      html.remove();
    }
    var arr = updateArray();
    var items = arr.filter(function(v) {
      return v.selected;
    });
    html = $(mustache.render(options.template, {items: items}));
    select.after(html);
  }
   accepts element and toggles the index of the option
   element to de-select. Removes tag
  function remove(element) {
    var index = element.getAttribute('data-index');
    select[0].options[index].selected = false;
    element.parentNode.parentNode.removeChild(element.parentNode);
  }
  var self = {
    init: init
  };
  return self;
}
function Tag ( element , config ) { $ . extend ( options , config ) var el = $ ( element ) ; var select = el . find ( 'select' ) ; var html ; function init ( ) { renderTags ( ) ; select . on ( 'change' , function ( ) { renderTags ( ) ; } ) ; el . on ( 'click' , '[data-tag]' , function ( event ) { event . preventDefault ( ) ; remove ( event . target ) ; } ) ; } function updateArray ( ) { var arr = [ ] ; select . find ( 'option' ) . each ( function ( i , v ) { arr . push ( { value : v . value , label : v . text , selected : v . selected , index : i } ) } ) ; return arr ; } function renderTags ( ) { if ( html ) { html . remove ( ) ; } var arr = updateArray ( ) ; var items = arr . filter ( function ( v ) { return v . selected ; } ) ; html = $ ( mustache . render ( options . template , { items : items } ) ) ; select . after ( html ) ; } function remove ( element ) { var index = element . getAttribute ( 'data-index' ) ; select [ 0 ] . options [ index ] . selected = false ; element . parentNode . parentNode . removeChild ( element . parentNode ) ; } var self = { init : init } ; return self ; }
Helper function to extend a <select multiple
 so as to create tags for assigning / priorities etc
Helper function to extend a <select multiple so as to create tags for assigning / priorities etc
// Private variables // base setup // delegate the tag close click to the container // @todo @refactor - weirdly, mapping returns on object rather than an array // of objects, so doing this like this temporarily // // accepts element and toggles the index of the option // element to de-select. Removes tag /**
   * Expose public methods
   */