mirror of
https://github.com/wahyd4/jQuery-Tags-Input.git
synced 2026-08-09 12:56:35 +10:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4f22220a36 | ||
|
|
8382bfc7f6 | ||
|
|
c5e24d6c89 | ||
|
|
238c8e7fa6 | ||
|
|
ef581e695a | ||
|
|
082acc53de | ||
|
|
fd1e79202f | ||
|
|
074d90a6c7 | ||
|
|
8c45811838 | ||
|
|
74520e1cbb | ||
|
|
ba418a2092 | ||
|
|
e227153125 | ||
|
|
86870134fb | ||
|
|
0844f11939 | ||
|
|
1dbfe86a79 | ||
|
|
0a94827e4d | ||
|
|
823a8b411e | ||
|
|
64e9860eff | ||
|
|
8ea9b4ea92 | ||
|
|
bfbc3181f2 | ||
|
|
0e4af3b60c | ||
|
|
375b385f46 | ||
|
|
1505a5d6b0 | ||
|
|
fbdaf2e84c | ||
|
|
8a67752cb4 | ||
|
|
c5637bf1f0 | ||
|
|
b2811f6f87 |
@@ -1,6 +1,6 @@
|
||||
jQuery Tags Input Plugin 1.2
|
||||
jQuery Tags Input Plugin 1.2.4
|
||||
|
||||
Copyright (c) 2010 XOXCO, Inc
|
||||
Copyright (c) 2011 XOXCO, Inc
|
||||
|
||||
Documentation for this plugin lives here:
|
||||
http://xoxco.com/clickable/jquery-tags-input
|
||||
@@ -8,6 +8,9 @@ http://xoxco.com/clickable/jquery-tags-input
|
||||
Licensed under the MIT license:
|
||||
http://www.opensource.org/licenses/mit-license.php
|
||||
|
||||
The git repository for this plugin can be found at:
|
||||
https://github.com/xoxco/jQuery-Tags-Input
|
||||
|
||||
ben@xoxco.com
|
||||
------------------------------------------------------
|
||||
|
||||
@@ -26,23 +29,29 @@ First, add the Javascript and CSS files to your <head> tag:
|
||||
<script src="jquery.tagsinput.js"></script>
|
||||
<link rel="stylesheet" type="text/css" href="jquery.tagsinput.css" />
|
||||
|
||||
Create a real input in your form that will contain a comma-separated list of tags.
|
||||
You can put any default or existing tags in the value attribute, and they'll be handled properly.
|
||||
Create a real input in your form that will contain a comma-separated list of
|
||||
tags. You can put any default or existing tags in the value attribute, and
|
||||
they'll be handled properly.
|
||||
|
||||
<input name="tags" id="tags" value="foo,bar,baz" />
|
||||
|
||||
Then, simply call the tagsInput function on any field that should be treated as a list of tags.
|
||||
Then, simply call the tagsInput function on any field that should be treated as
|
||||
a list of tags.
|
||||
|
||||
$('#tags').tagsInput();
|
||||
|
||||
If you want to use jQuery.autocomplete, you can pass in a parameter with the autocomplete url.
|
||||
If you want to use jQuery.autocomplete, you can pass in a parameter with the
|
||||
autocomplete url.
|
||||
|
||||
$('#tags').tagsInput({autocomplete_url:'http://myserver.com/api/autocomplete'});
|
||||
$('#tags').tagsInput({
|
||||
autocomplete_url:'http://myserver.com/api/autocomplete'
|
||||
});
|
||||
|
||||
You can also send in options to the autocomplete plugin, as described here.
|
||||
|
||||
$('#tags').tagsInput({ autocomplete_url:'http://myserver.com/api/autocomplete',
|
||||
autocomplete:{selectFirst:true,width:'100px',autoFill:true}
|
||||
$('#tags').tagsInput({
|
||||
autocomplete_url:'http://myserver.com/api/autocomplete',
|
||||
autocomplete:{selectFirst:true,width:'100px',autoFill:true}
|
||||
});
|
||||
|
||||
You can add and remove tags by calling the addTag() and removeTag() functions.
|
||||
@@ -50,6 +59,22 @@ You can add and remove tags by calling the addTag() and removeTag() functions.
|
||||
$('#tags').addTag('foo');
|
||||
$('#tags').removeTag('bar');
|
||||
|
||||
If additional functionality is required when a tag is added or removed, you may
|
||||
specify callback functions via the onAddTag and onRemoveTag parameters. Both
|
||||
functions should accept a single tag as the parameter.
|
||||
|
||||
If you do not want to provide a way to add tags, or you would prefer to provide
|
||||
an alternate interface for adding tags to the box, you may pass an false into
|
||||
the optional 'interactive' parameter. The tags will still be rendered as per
|
||||
usual, and the addTag and removeTag functions will operate as expected.
|
||||
|
||||
If you want a function to be called every time a tag is updated/deleted, set it
|
||||
as the 'onChange' option.
|
||||
|
||||
By default, if the cursor is immediately after a tag, hitting backspace will
|
||||
delete that tag. If you want to override this, set the 'removeWithBackspace'
|
||||
option to false.
|
||||
|
||||
Options
|
||||
|
||||
$(selector).tagsInput({
|
||||
@@ -57,7 +82,12 @@ $(selector).tagsInput({
|
||||
'autocomplete': { option: value, option: value},
|
||||
'height':'100px',
|
||||
'width':'300px',
|
||||
'interactive':true,
|
||||
'defaultText':'add a tag',
|
||||
'onAddTag':callback_function,
|
||||
'onRemoveTag':callback_function,
|
||||
'onChange' : callback_function,
|
||||
'removeWithBackspace' : true,
|
||||
'minChars' : 0,
|
||||
'maxChars' : 0 //if not provided there is no limit
|
||||
});
|
||||
|
||||
+132
-95
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
|
||||
jQuery Tags Input Plugin 1.2.3
|
||||
jQuery Tags Input Plugin 1.2.4
|
||||
|
||||
Copyright (c) 2010 XOXCO, Inc
|
||||
|
||||
@@ -17,10 +17,10 @@
|
||||
(function($) {
|
||||
|
||||
var delimiter = new Array();
|
||||
var tags_callbacks = new Array();
|
||||
|
||||
jQuery.fn.addTag = function(value,options) {
|
||||
|
||||
var options = jQuery.extend({focus:false},options);
|
||||
$.fn.addTag = function(value,options) {
|
||||
var options = jQuery.extend({focus:false,callback:true},options);
|
||||
this.each(function() {
|
||||
id = $(this).attr('id');
|
||||
|
||||
@@ -48,16 +48,27 @@
|
||||
} else {
|
||||
$('#'+id+'_tag').blur();
|
||||
}
|
||||
|
||||
if (options.callback && tags_callbacks[id] && tags_callbacks[id]['onAddTag']) {
|
||||
var f = tags_callbacks[id]['onAddTag'];
|
||||
f(value);
|
||||
}
|
||||
if(tags_callbacks[id] && tags_callbacks[id]['onChange'])
|
||||
{
|
||||
var i = tagslist.length;
|
||||
var f = tags_callbacks[id]['onChange'];
|
||||
f($(this), tagslist[i]);
|
||||
}
|
||||
}
|
||||
jQuery.fn.tagsInput.updateTagsField(this,tagslist);
|
||||
$.fn.tagsInput.updateTagsField(this,tagslist);
|
||||
|
||||
});
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
jQuery.fn.removeTag = function(value) {
|
||||
|
||||
$.fn.removeTag = function(value) {
|
||||
value = unescape(value);
|
||||
this.each(function() {
|
||||
id = $(this).attr('id');
|
||||
|
||||
@@ -67,20 +78,23 @@
|
||||
$('#'+id+'_tagsinput .tag').remove();
|
||||
str = '';
|
||||
for (i=0; i< old.length; i++) {
|
||||
if (escape(old[i])!=value) {
|
||||
if (old[i]!=value) {
|
||||
str = str + delimiter[id] +old[i];
|
||||
}
|
||||
}
|
||||
|
||||
jQuery.fn.tagsInput.importTags(this,str);
|
||||
$.fn.tagsInput.importTags(this,str);
|
||||
|
||||
if (tags_callbacks[id] && tags_callbacks[id]['onRemoveTag']) {
|
||||
var f = tags_callbacks[id]['onRemoveTag'];
|
||||
f(value);
|
||||
}
|
||||
});
|
||||
|
||||
return false;
|
||||
|
||||
};
|
||||
|
||||
jQuery.fn.tagExist = function(val) {
|
||||
|
||||
$.fn.tagExist = function(val) {
|
||||
if (jQuery.inArray(val, $(this)) == -1) {
|
||||
return false; /* Cannot find value in array */
|
||||
} else {
|
||||
@@ -89,14 +103,13 @@
|
||||
};
|
||||
|
||||
// clear all existing tags and import new ones from a string
|
||||
jQuery.fn.importTags = function(str) {
|
||||
$.fn.importTags = function(str) {
|
||||
$('#'+id+'_tagsinput .tag').remove();
|
||||
jQuery.fn.tagsInput.importTags(this,str);
|
||||
$.fn.tagsInput.importTags(this,str);
|
||||
}
|
||||
|
||||
jQuery.fn.tagsInput = function(options) {
|
||||
|
||||
var settings = jQuery.extend({defaultText:'add a tag',minChars:0,width:'300px',height:'100px','hide':true,'delimiter':',',autocomplete:{selectFirst:false},'unique':true},options);
|
||||
$.fn.tagsInput = function(options) {
|
||||
var settings = jQuery.extend({interactive:true,defaultText:'add a tag',minChars:0,width:'300px',height:'100px','hide':true,'delimiter':',',autocomplete:{selectFirst:false},'unique':true,removeWithBackspace:true},options);
|
||||
|
||||
this.each(function() {
|
||||
if (settings.hide) {
|
||||
@@ -115,79 +128,104 @@
|
||||
|
||||
|
||||
delimiter[id] = data.delimiter;
|
||||
|
||||
if (settings.onAddTag || settings.onRemoveTag || settings.onChange) {
|
||||
tags_callbacks[id] = new Array();
|
||||
tags_callbacks[id]['onAddTag'] = settings.onAddTag;
|
||||
tags_callbacks[id]['onRemoveTag'] = settings.onRemoveTag;
|
||||
tags_callbacks[id]['onChange'] = settings.onChange;
|
||||
}
|
||||
|
||||
$('<div id="'+id+'_tagsinput" class="tagsinput"><div id="'+id+'_addTag"><input id="'+id+'_tag" value="" default="'+settings.defaultText+'" /></div><div class="tags_clear"></div></div>').insertAfter(this);
|
||||
|
||||
var markup = '<div id="'+id+'_tagsinput" class="tagsinput"><div id="'+id+'_addTag">';
|
||||
|
||||
if (settings.interactive) {
|
||||
markup = markup + '<input id="'+id+'_tag" value="" data-default="'+settings.defaultText+'" />';
|
||||
}
|
||||
|
||||
markup = markup + '</div><div class="tags_clear"></div></div>';
|
||||
|
||||
$(markup).insertAfter(this);
|
||||
|
||||
$(data.holder).css('width',settings.width);
|
||||
$(data.holder).css('height',settings.height);
|
||||
|
||||
|
||||
if ($(data.real_input).val()!='') {
|
||||
jQuery.fn.tagsInput.importTags($(data.real_input),$(data.real_input).val());
|
||||
} else {
|
||||
$(data.fake_input).val($(data.fake_input).attr('default'));
|
||||
$(data.fake_input).css('color','#666666');
|
||||
}
|
||||
$.fn.tagsInput.importTags($(data.real_input),$(data.real_input).val());
|
||||
}
|
||||
if (settings.interactive) {
|
||||
$(data.fake_input).val($(data.fake_input).attr('data-default'));
|
||||
$(data.fake_input).css('color','#666666');
|
||||
|
||||
|
||||
$(data.holder).bind('click',data,function(event) {
|
||||
$(event.data.fake_input).focus();
|
||||
});
|
||||
|
||||
$(data.fake_input).bind('focus',data,function(event) {
|
||||
if ($(event.data.fake_input).val()==$(event.data.fake_input).attr('default')) {
|
||||
$(event.data.fake_input).val('');
|
||||
}
|
||||
$(event.data.fake_input).css('color','#000000');
|
||||
});
|
||||
|
||||
if (settings.autocomplete_url != undefined) {
|
||||
$(data.fake_input).autocomplete(settings.autocomplete_url,settings.autocomplete).bind('result',data,function(event,data,formatted) {
|
||||
if (data)
|
||||
{
|
||||
$(event.data.real_input).addTag(formatted,{focus:true,unique:(settings.unique)});
|
||||
}
|
||||
$(data.holder).bind('click',data,function(event) {
|
||||
$(event.data.fake_input).focus();
|
||||
});
|
||||
|
||||
$(data.fake_input).bind('blur',data,function(event) {
|
||||
if( $('.ac_results').is(':visible') ) return false;
|
||||
if ( $(event.data.fake_input).val() != $(event.data.fake_input).attr('default')) {
|
||||
if((event.data.minChars <= $(event.data.fake_input).val().length) && (!event.data.maxChars || (event.data.maxChars >= $(event.data.fake_input).val().length)))
|
||||
$(event.data.real_input).addTag($(event.data.fake_input).val(),{focus:false,unique:(settings.unique)});
|
||||
|
||||
$(data.fake_input).bind('focus',data,function(event) {
|
||||
if ($(event.data.fake_input).val()==$(event.data.fake_input).attr('data-default')) {
|
||||
$(event.data.fake_input).val('');
|
||||
}
|
||||
|
||||
$(event.data.fake_input).val($(event.data.fake_input).attr('default'));
|
||||
$(event.data.fake_input).css('color','#666666');
|
||||
return false;
|
||||
$(event.data.fake_input).css('color','#000000');
|
||||
});
|
||||
|
||||
|
||||
} else {
|
||||
// if a user tabs out of the field, create a new tag
|
||||
// this is only available if autocomplete is not used.
|
||||
$(data.fake_input).bind('blur',data,function(event) {
|
||||
var d = $(this).attr('default');
|
||||
if ($(event.data.fake_input).val()!='' && $(event.data.fake_input).val()!=d) {
|
||||
if( (event.data.minChars <= $(event.data.fake_input).val().length) && (!event.data.maxChars || (event.data.maxChars >= $(event.data.fake_input).val().length)) )
|
||||
$(event.data.real_input).addTag($(event.data.fake_input).val(),{focus:true,unique:(settings.unique)});
|
||||
} else {
|
||||
$(event.data.fake_input).val($(event.data.fake_input).attr('default'));
|
||||
$(event.data.fake_input).css('color','#666666');
|
||||
|
||||
if (settings.autocomplete_url != undefined) {
|
||||
$(data.fake_input).autocomplete(settings.autocomplete_url,settings.autocomplete).bind('result',data,function(event,data,formatted) {
|
||||
if (data)
|
||||
{
|
||||
$(event.data.real_input).addTag(formatted,{focus:true,unique:(settings.unique)});
|
||||
}
|
||||
});
|
||||
|
||||
$(data.fake_input).bind('blur',data,function(event) {
|
||||
if( $('.ac_results').is(':visible') ) return false;
|
||||
if ( $(event.data.fake_input).val() != $(event.data.fake_input).attr('data-default')) {
|
||||
if((event.data.minChars <= $(event.data.fake_input).val().length) && (!event.data.maxChars || (event.data.maxChars >= $(event.data.fake_input).val().length)))
|
||||
$(event.data.real_input).addTag($(event.data.fake_input).val(),{focus:false,unique:(settings.unique)});
|
||||
}
|
||||
|
||||
$(event.data.fake_input).val($(event.data.fake_input).attr('data-default'));
|
||||
$(event.data.fake_input).css('color','#666666');
|
||||
return false;
|
||||
});
|
||||
|
||||
}
|
||||
// if user types a comma, create a new tag
|
||||
$(data.fake_input).bind('keypress',data,function(event) {
|
||||
if (event.which==event.data.delimiter.charCodeAt(0) || event.which==13 ) {
|
||||
if( (event.data.minChars <= $(event.data.fake_input).val().length) && (!event.data.maxChars || (event.data.maxChars >= $(event.data.fake_input).val().length)) )
|
||||
$(event.data.real_input).addTag($(event.data.fake_input).val(),{focus:true,unique:(settings.unique)});
|
||||
|
||||
return false;
|
||||
} else {
|
||||
// if a user tabs out of the field, create a new tag
|
||||
// this is only available if autocomplete is not used.
|
||||
$(data.fake_input).bind('blur',data,function(event) {
|
||||
var d = $(this).attr('data-default');
|
||||
if ($(event.data.fake_input).val()!='' && $(event.data.fake_input).val()!=d) {
|
||||
if( (event.data.minChars <= $(event.data.fake_input).val().length) && (!event.data.maxChars || (event.data.maxChars >= $(event.data.fake_input).val().length)) )
|
||||
$(event.data.real_input).addTag($(event.data.fake_input).val(),{focus:true,unique:(settings.unique)});
|
||||
} else {
|
||||
$(event.data.fake_input).val($(event.data.fake_input).attr('data-default'));
|
||||
$(event.data.fake_input).css('color','#666666');
|
||||
}
|
||||
return false;
|
||||
});
|
||||
|
||||
}
|
||||
});
|
||||
$(data.fake_input).blur();
|
||||
// if user types a comma, create a new tag
|
||||
$(data.fake_input).bind('keypress',data,function(event) {
|
||||
if (event.which==event.data.delimiter.charCodeAt(0) || event.which==13 ) {
|
||||
if( (event.data.minChars <= $(event.data.fake_input).val().length) && (!event.data.maxChars || (event.data.maxChars >= $(event.data.fake_input).val().length)) )
|
||||
$(event.data.real_input).addTag($(event.data.fake_input).val(),{focus:true,unique:(settings.unique)});
|
||||
|
||||
return false;
|
||||
}
|
||||
});
|
||||
//Delete last tag on backspace
|
||||
data.removeWithBackspace && $(data.fake_input).bind('keyup', function(event)
|
||||
{
|
||||
if(event.keyCode == 8 && $(this).val() == '')
|
||||
{
|
||||
var last_tag = $(this).closest('.tagsinput').find('.tag:last').text();
|
||||
var id = $(this).attr('id').replace(/_tag$/, '');
|
||||
last_tag = last_tag.replace(/[\s]+x$/, '');
|
||||
$('#' + id).removeTag(escape(last_tag));
|
||||
$(this).trigger('focus');
|
||||
};
|
||||
});
|
||||
$(data.fake_input).blur();
|
||||
} // if settings.interactive
|
||||
return false;
|
||||
});
|
||||
|
||||
@@ -195,24 +233,23 @@
|
||||
|
||||
};
|
||||
|
||||
$.fn.tagsInput.updateTagsField = function(obj,tagslist) {
|
||||
id = $(obj).attr('id');
|
||||
$(obj).val(tagslist.join(delimiter[id]));
|
||||
};
|
||||
|
||||
jQuery.fn.tagsInput.updateTagsField = function(obj,tagslist) {
|
||||
|
||||
id = $(obj).attr('id');
|
||||
$(obj).val(tagslist.join(delimiter[id]));
|
||||
};
|
||||
|
||||
|
||||
|
||||
jQuery.fn.tagsInput.importTags = function(obj,val) {
|
||||
|
||||
$(obj).val('');
|
||||
id = $(obj).attr('id');
|
||||
var tags = val.split(delimiter[id]);
|
||||
for (i=0; i<tags.length; i++) {
|
||||
$(obj).addTag(tags[i],{focus:false});
|
||||
}
|
||||
};
|
||||
$.fn.tagsInput.importTags = function(obj,val) {
|
||||
$(obj).val('');
|
||||
id = $(obj).attr('id');
|
||||
var tags = val.split(delimiter[id]);
|
||||
for (i=0; i<tags.length; i++) {
|
||||
$(obj).addTag(tags[i],{focus:false,callback:false});
|
||||
}
|
||||
if(tags_callbacks[id] && tags_callbacks[id]['onChange'])
|
||||
{
|
||||
var f = tags_callbacks[id]['onChange'];
|
||||
f(obj, tags[i]);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
})(jQuery);
|
||||
})(jQuery);
|
||||
Vendored
+1
File diff suppressed because one or more lines are too long
@@ -11,8 +11,32 @@
|
||||
|
||||
<script type="text/javascript">
|
||||
|
||||
function onAddTag(tag) {
|
||||
alert("Added a tag: " + tag);
|
||||
}
|
||||
function onRemoveTag(tag) {
|
||||
alert("Removed a tag: " + tag);
|
||||
}
|
||||
|
||||
$(function() {
|
||||
$('input.tags').tagsInput();
|
||||
$('#tags_1').tagsInput();
|
||||
$('#tags_2').tagsInput({
|
||||
onChange: function(elem, elem_tags)
|
||||
{
|
||||
var languages = ['php','ruby','javascript'];
|
||||
$('.tag', elem_tags).each(function()
|
||||
{
|
||||
if($(this).text().search(new RegExp('\\b(' + languages.join('|') + ')\\b')) >= 0)
|
||||
$(this).css('background-color', 'yellow');
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
// Uncomment this line to see the callback functions in action
|
||||
// $('input.tags').tagsInput({onAddTag:onAddTag,onRemoveTag:onRemoveTag});
|
||||
|
||||
// Uncomment this line to see an input with no interface for adding new tags.
|
||||
// $('input.tags').tagsInput({interactive:false});
|
||||
});
|
||||
|
||||
</script>
|
||||
@@ -25,6 +49,9 @@
|
||||
<section>
|
||||
<form>
|
||||
<input id="tags_1" type="text" class="tags" value="foo,bar,baz,roffle" />
|
||||
|
||||
<label>Technologies: (Programming languages in yellow)</label>
|
||||
<input id="tags_2" type="text" class="tags" value="php,ios,javascript,ruby,android,kindle" />
|
||||
</form>
|
||||
</section>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user