Skip to content

Instantly share code, notes, and snippets.

@joeljunstrom
Created June 23, 2010 15:15
Show Gist options
  • Save joeljunstrom/450064 to your computer and use it in GitHub Desktop.
Save joeljunstrom/450064 to your computer and use it in GitHub Desktop.
$.fn.tagList = function() {
return this.each(function() {
var $orginal = $(this),
$input = $('<input type="text">'),
$button = $('<button class="add_tag">Add</button>'),
$tag_list = $('<ul class="tags" />'),
$suggestions = $orginal.parents('form').find('div.popular.tags'),
current_value = $orginal.val();
$orginal.parent()
.addClass('taggings')
.prepend($tag_list)
.append($button)
.end()
.replaceWith($input);
$tag_list.data('tags', []);
$tag_list.bind('add.tags', function(event, value) {
var tag_items = '';
$.each(value.split(' '), function() {
var tag = $.trim(this).toLowerCase();
if ($.inArray(tag, $tag_list.data('tags')) == -1) {
$tag_list.data('tags').push(tag);
tag_items += '<li><span>'+tag+'</span><input type="hidden" name="mash_app[tag_list][]" value="'+tag+'"><a href="#remove_tag" class="remove">-</a></li>';
}
});
$tag_list.append(tag_items);
}).bind('click.tags', function(event) {
if (event.target.nodeName.toLowerCase() != 'a') { return; }
event.preventDefault();
var $item = $(event.target).parent('li'),
tag = $item.find('span').text(),
index = $.inArray(tag, $tag_list.data('tags'));
$tag_list.data('tags').splice(index, 1);
$item.remove(); // This call generates a "no method or property for object" error in IE
// this.removeChild(event.target.parentNode) // so does this
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment