diff --git a/README.md b/README.md index bd0dcb5..84455d0 100644 --- a/README.md +++ b/README.md @@ -95,6 +95,7 @@ option to false. 'onAddTag':callback_function, 'onRemoveTag':callback_function, 'onChange' : callback_function, + 'delimiter': [',',';'], 'removeWithBackspace' : true, 'minChars' : 0, 'maxChars' : 0 //if not provided there is no limit, diff --git a/example.html b/example.html index 2dea0a5..d2057af 100644 --- a/example.html +++ b/example.html @@ -1,7 +1,7 @@ - + - + - - + +

- +

- +

- -
\ No newline at end of file + + diff --git a/jquery.tagsinput.js b/jquery.tagsinput.js index dd39357..cdff353 100644 --- a/jquery.tagsinput.js +++ b/jquery.tagsinput.js @@ -1,12 +1,12 @@ /* jQuery Tags Input Plugin 1.3.3 - + Copyright (c) 2011 XOXCO, Inc - + Documentation for this plugin lives here: http://xoxco.com/clickable/jquery-tags-input - + Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php @@ -24,9 +24,9 @@ val = '', input = $(this), testSubject = $('#'+$(this).data('tester_id')); - + if (val === (val = input.val())) {return;} - + // Enter new content into testSubject var escaped = val.replace(/&/g, '&').replace(/\s/g,' ').replace(//g, '>'); testSubject.html(escaped); @@ -36,7 +36,7 @@ currentWidth = input.width(), isValidWidthChange = (newWidth < currentWidth && newWidth >= minWidth) || (newWidth > minWidth && newWidth < maxWidth); - + // Animate width if (isValidWidthChange) { input.width(newWidth); @@ -72,19 +72,19 @@ input.data('tester_id', testerId); input.css('width', minWidth); }; - + $.fn.addTag = function(value,options) { options = jQuery.extend({focus:false,callback:true},options); - this.each(function() { + this.each(function() { var id = $(this).attr('id'); var tagslist = $(this).val().split(delimiter[id]); - if (tagslist[0] == '') { + if (tagslist[0] == '') { tagslist = new Array(); } value = jQuery.trim(value); - + if (options.unique) { var skipTag = $(this).tagExist(value); if(skipTag == true) { @@ -92,10 +92,10 @@ $('#'+id+'_tag').addClass('not_valid'); } } else { - var skipTag = false; + var skipTag = false; } - - if (value !='' && skipTag != true) { + + if (value !='' && skipTag != true) { $('').addClass('tag').append( $('').text(value).append('  '), $('', { @@ -108,16 +108,16 @@ ).insertBefore('#' + id + '_addTag'); tagslist.push(value); - + $('#'+id+'_tag').val(''); if (options.focus) { $('#'+id+'_tag').focus(); - } else { + } else { $('#'+id+'_tag').blur(); } - + $.fn.tagsInput.updateTagsField(this,tagslist); - + if (options.callback && tags_callbacks[id] && tags_callbacks[id]['onAddTag']) { var f = tags_callbacks[id]['onAddTag']; f.call(this, value); @@ -127,29 +127,29 @@ var i = tagslist.length; var f = tags_callbacks[id]['onChange']; f.call(this, $(this), tagslist[i-1]); - } + } } - - }); - + + }); + return false; }; - - $.fn.removeTag = function(value) { + + $.fn.removeTag = function(value) { value = unescape(value); - this.each(function() { + this.each(function() { var id = $(this).attr('id'); - + var old = $(this).val().split(delimiter[id]); - + $('#'+id+'_tagsinput .tag').remove(); str = ''; - for (i=0; i< old.length; i++) { - if (old[i]!=value) { + for (i=0; i< old.length; i++) { + if (old[i]!=value) { str = str + delimiter[id] +old[i]; } } - + $.fn.tagsInput.importTags(this,str); if (tags_callbacks[id] && tags_callbacks[id]['onRemoveTag']) { @@ -157,24 +157,24 @@ f.call(this, value); } }); - + return false; }; - + $.fn.tagExist = function(val) { var id = $(this).attr('id'); var tagslist = $(this).val().split(delimiter[id]); return (jQuery.inArray(val, tagslist) >= 0); //true when tag exists, false when not }; - + // clear all existing tags and import new ones from a string $.fn.importTags = function(str) { id = $(this).attr('id'); $('#'+id+'_tagsinput .tag').remove(); $.fn.tagsInput.importTags(this,str); } - - $.fn.tagsInput = function(options) { + + $.fn.tagsInput = function(options) { var settings = jQuery.extend({ interactive:true, defaultText:'add a tag', @@ -182,9 +182,9 @@ width:'300px', height:'100px', autocomplete: {selectFirst: false }, - 'hide':true, - 'delimiter':',', - 'unique':true, + hide:true, + delimiter:[',',';'], + unique:true, removeWithBackspace:true, placeholderColor:'#666666', autosize: true, @@ -192,15 +192,15 @@ inputPadding: 6*2 },options); - this.each(function() { - if (settings.hide) { - $(this).hide(); + this.each(function() { + if (settings.hide) { + $(this).hide(); } var id = $(this).attr('id'); if (!id || delimiter[$(this).attr('id')]) { id = $(this).attr('id', 'tags' + new Date().getTime()).attr('id'); } - + var data = jQuery.extend({ pid:id, real_input: '#'+id, @@ -208,55 +208,55 @@ input_wrapper: '#'+id+'_addTag', fake_input: '#'+id+'_tag' },settings); - + 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; } - + var markup = '
'; - + if (settings.interactive) { markup = markup + ''; } - + markup = markup + '
'; - + $(markup).insertAfter(this); $(data.holder).css('width',settings.width); $(data.holder).css('min-height',settings.height); - $(data.holder).css('height','100%'); - - if ($(data.real_input).val()!='') { + $(data.holder).css('height',settings.height); + + if ($(data.real_input).val()!='') { $.fn.tagsInput.importTags($(data.real_input),$(data.real_input).val()); - } - if (settings.interactive) { + } + if (settings.interactive) { $(data.fake_input).val($(data.fake_input).attr('data-default')); $(data.fake_input).css('color',settings.placeholderColor); $(data.fake_input).resetAutosize(settings); - + $(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('data-default')) { + if ($(event.data.fake_input).val()==$(event.data.fake_input).attr('data-default')) { $(event.data.fake_input).val(''); } - $(event.data.fake_input).css('color','#000000'); + $(event.data.fake_input).css('color','#000000'); }); - + if (settings.autocomplete_url != undefined) { autocomplete_options = {source: settings.autocomplete_url}; - for (attrname in settings.autocomplete) { - autocomplete_options[attrname] = settings.autocomplete[attrname]; + for (attrname in settings.autocomplete) { + autocomplete_options[attrname] = settings.autocomplete[attrname]; } - + if (jQuery.Autocompleter !== undefined) { $(data.fake_input).autocomplete(settings.autocomplete_url, settings.autocomplete); $(data.fake_input).bind('result',data,function(event,data,formatted) { @@ -271,14 +271,14 @@ 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) { + $(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.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 { @@ -287,11 +287,11 @@ } return false; }); - + } - // if user types a comma, create a new tag + // if user types a default delimiter like comma,semicolon and then create a new tag $(data.fake_input).bind('keypress',data,function(event) { - if (event.which==event.data.delimiter.charCodeAt(0) || event.which==13 ) { + if (_checkDelimiter(event)) { event.preventDefault(); 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)}); @@ -299,7 +299,7 @@ return false; } else if (event.data.autosize) { $(event.data.fake_input).doAutosize(settings); - + } }); //Delete last tag on backspace @@ -316,7 +316,7 @@ } }); $(data.fake_input).blur(); - + //Removes the not_valid class when user changes the value of the fake input if(data.unique) { $(data.fake_input).keydown(function(event){ @@ -327,21 +327,21 @@ } } // if settings.interactive }); - + return this; - + }; - - $.fn.tagsInput.updateTagsField = function(obj,tagslist) { + + $.fn.tagsInput.updateTagsField = function(obj,tagslist) { var id = $(obj).attr('id'); $(obj).val(tagslist.join(delimiter[id])); }; - - $.fn.tagsInput.importTags = function(obj,val) { + + $.fn.tagsInput.importTags = function(obj,val) { $(obj).val(''); var id = $(obj).attr('id'); var tags = val.split(delimiter[id]); - for (i=0; i