From f39adfaf7706afcc9670b34096c9394a7bd10bfa Mon Sep 17 00:00:00 2001 From: Alex Ehlke Date: Fri, 23 Nov 2012 01:11:01 -0500 Subject: [PATCH] add removeTagByName --- README.markdown | 5 +++++ examples.html | 1 + js/tag-it.js | 8 ++++++++ js/tag-it.min.js | 3 ++- 4 files changed, 16 insertions(+), 1 deletion(-) diff --git a/README.markdown b/README.markdown index b4bd081f..0c5ebad3 100644 --- a/README.markdown +++ b/README.markdown @@ -201,6 +201,11 @@ Adds new tag to the list. The `additionalClass` parameter is an optional way to $("#mytags").tagit("createTag", "brand-new-tag"); +### removeTagByName(tagName, animate) +Finds the tag with the value `tagName` and removes it. If no such tag is found, it'll throw an exception. + + $("#mytags").tagit("removeTagByName", "my-tag"); + ### removeAll() Clears the widget of all tags -- removes each tag it contains, so the onTagRemoved event callback (if set in the options) will be called for each. diff --git a/examples.html b/examples.html index d8ad8096..b72bac9a 100644 --- a/examples.html +++ b/examples.html @@ -203,6 +203,7 @@

Methods

Demos the available widget methods. Click the links below the widget to try them.

Create tag

+

Remove tag by name

Clear tags

diff --git a/js/tag-it.js b/js/tag-it.js index 2017bbc1..bcdd162c 100644 --- a/js/tag-it.js +++ b/js/tag-it.js @@ -391,6 +391,14 @@ } }, + removeTagByName: function(tagName, animate) { + var toRemove = this._tags().find("input[value='" + tagName + "']").closest('.tagit-choice'); + if (toRemove.length === 0) { + throw "No such tag exists with the name '" + tagName + "'"; + } + this.removeTag(toRemove, animate); + }, + removeAll: function() { // Removes all tags. var that = this; diff --git a/js/tag-it.min.js b/js/tag-it.min.js index 9696f26e..967b49c9 100644 --- a/js/tag-it.min.js +++ b/js/tag-it.min.js @@ -8,4 +8,5 @@ a.removeTag(a._lastTag(),!1);a.createTag(c.item.value);return!1}})},_cleanedInpu return c},_updateSingleTagsField:function(a){b(this.options.singleFieldNode).val(a.join(this.options.singleFieldDelimiter))},_subtractArray:function(a,c){for(var d=[],e=0;e':'').text(a),f=b("
  • ").addClass("tagit-choice ui-widget-content ui-state-default ui-corner-all").addClass(c).append(e),g=b("").addClass("ui-icon ui-icon-close"),g=b('\u00d7').addClass("tagit-close").append(g).click(function(){d.removeTag(f)}); f.append(g);this.options.singleField?(e=this.assignedTags(),e.push(a),this._updateSingleTagsField(e)):(e=e.html(),f.append(''));this._trigger("onTagAdded",null,f);this._tagInput.val("");this._tagInput.parent().before(f)},removeTag:function(a,c){c="undefined"===typeof c?this.options.animate:c;a=b(a);this._trigger("onTagRemoved",null,a);if(this.options.singleField){var d=this.assignedTags(), -e=this.tagLabel(a),d=b.grep(d,function(a){return a!=e});this._updateSingleTagsField(d)}c?(d=b.effects&&b.effects.blind?["blind",{direction:"horizontal"},"fast"]:["fast"],d.push(function(){a.remove()}),a.fadeOut("fast").hide.apply(a,d).dequeue()):a.remove()},removeAll:function(){var a=this;this._tags().each(function(b,d){a.removeTag(d,!1)})}})})(jQuery); +e=this.tagLabel(a),d=b.grep(d,function(a){return a!=e});this._updateSingleTagsField(d)}c?(d=b.effects&&b.effects.blind?["blind",{direction:"horizontal"},"fast"]:["fast"],d.push(function(){a.remove()}),a.fadeOut("fast").hide.apply(a,d).dequeue()):a.remove()},removeTagByName:function(a,b){var d=this._tags().find("input[value='"+a+"']").closest(".tagit-choice");if(0===d.length)throw"No such tag exists with the name '"+a+"'";this.removeTag(d,b)},removeAll:function(){var a=this;this._tags().each(function(b, +d){a.removeTag(d,!1)})}})})(jQuery);