30 Commits
Author SHA1 Message Date
Sebastian Motraghi 240f4274a0 bump to version 1.2.5 2011-06-02 09:37:48 +01:00
Sebastian Motraghi 14a01fa89f minified punytan's changes. 2011-06-02 09:32:54 +01:00
punytan 9b584de09a fix the problem around unsafe chars 2011-06-01 20:23:46 +09:00
Sebastian Motraghi 4f22220a36 update docs; bump version number 2011-05-25 15:40:36 +01:00
Sebastian Motraghi 8382bfc7f6 new minified version 2011-05-25 15:40:20 +01:00
Sebastian Motraghi c5e24d6c89 onChange / removeWithBackspace documentation 2011-05-25 15:26:12 +01:00
Sebastian Motraghi 238c8e7fa6 wrap README lines 2011-05-25 15:25:38 +01:00
Sebastian Motraghi ef581e695a Merge pull request #9 from sebm/master
Incorporate @marioestrada's onChange / backspace changes
2011-05-25 07:06:09 -07:00
Sebastian Motraghi 082acc53de Call the 'onChange' function when a tag is added. 2011-05-25 14:57:03 +01:00
Mario Estrada fd1e79202f added new test for onChange, changed functionality to send the generated tag element and the original input to onChange 2011-05-24 19:36:05 -05:00
Mario Estrada 074d90a6c7 removed extra whitespace 2011-05-24 19:23:13 -05:00
Mario Estrada 8c45811838 Replace jQuery.fn with $.fn since it is already wrapped, added onChange to be called after adding or removing a tag 2011-05-24 19:22:06 -05:00
Mario Estrada 74520e1cbb Added support for remove tack with backspace 2011-05-20 19:29:57 -05:00
Ben Brown ba418a2092 Bug fix pertaining to over-escaping tags. 2011-04-28 16:49:49 -05:00
Ben Brown e227153125 Minified. 2011-04-28 16:09:17 -05:00
Ben Brown 86870134fb Adds the 'interactive' parameter, defaulted to true.
If interactive==true, an input will be inserted, allowing users to type new tags.

If interactive==false, no input will be inserted, and tags must be added using the addTags function.
2011-04-28 16:08:01 -05:00
Ben Brown 0844f11939 Updated minified version 2011-04-28 15:09:21 -05:00
Ben Brown 1dbfe86a79 Options 2011-04-28 15:08:14 -05:00
Ben Brown 0a94827e4d Merge branch 'master' of github.com:xoxco/jQuery-Tags-Input 2011-04-28 15:07:45 -05:00
Ben Brown 823a8b411e Only call addTag once, BEN. 2011-04-28 15:05:20 -05:00
Ben Brown 64e9860eff When importing tags, do NOT call the callback. 2011-04-28 15:02:06 -05:00
Ben Brown 8ea9b4ea92 Store the callback functions for later. 2011-04-28 15:01:08 -05:00
Ben Brown bfbc3181f2 If an onRemoveTag callback has been specified, call it 2011-04-28 15:00:50 -05:00
Ben Brown 0e4af3b60c If a callback has been specified, call it when a tag is added. 2011-04-28 15:00:27 -05:00
Ben Brown 375b385f46 Store the callbacks 2011-04-28 15:00:00 -05:00
Ben Brown 1505a5d6b0 By default, if a callback has been specified, call it! 2011-04-28 14:59:44 -05:00
Ben Brown fbdaf2e84c Added sample onAddTag and onRemoveTag callbacks. 2011-04-28 14:59:12 -05:00
Ben Brown 8a67752cb4 Added info for new callback function parameters 2011-04-28 14:58:58 -05:00
Ben Brown c5637bf1f0 Merged pull request #3 from devbrothers/master.
Added minified version.
2011-04-26 16:03:20 -07:00
Bartosz Grzybowski b2811f6f87 added minified version 2011-04-19 12:47:41 +02:00
4 changed files with 211 additions and 107 deletions
+39 -9
View File
@@ -1,6 +1,6 @@
jQuery Tags Input Plugin 1.2
jQuery Tags Input Plugin 1.2.5
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
});
+143 -97
View File
@@ -1,8 +1,8 @@
/*
jQuery Tags Input Plugin 1.2.3
jQuery Tags Input Plugin 1.2.5
Copyright (c) 2010 XOXCO, Inc
Copyright (c) 2011 XOXCO, Inc
Documentation for this plugin lives here:
http://xoxco.com/clickable/jquery-tags-input
@@ -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');
@@ -38,8 +38,17 @@
}
if (value !='' && skipTag != true) {
$('<span class="tag">'+value + '&nbsp;&nbsp;<a href="#" title="Remove tag" onclick="return $(\'#'+id + '\').removeTag(\'' + escape(value) + '\');">x</a></span>').insertBefore('#'+id+'_addTag');
$('<span>').addClass('tag').append(
$('<span>').text(value).append('&nbsp;&nbsp;'),
$('<a>', {
href : '#',
title : 'Removing tag',
text : 'x'
}).click(function () {
return $('#' + id).removeTag(escape(value));
})
).insertBefore('#' + id + '_addTag');
tagslist.push(value);
$('#'+id+'_tag').val('');
@@ -48,16 +57,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 +87,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 +112,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 +137,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 +242,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);
+1
View File
File diff suppressed because one or more lines are too long
+28 -1
View File
@@ -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>