Tagify

Intro

Tagify is a simple Prototype based script, which allows you to input tags and give the user a visual feedback. If you have questions or feedback feel free to comment on this blog post: Tagify - a tagging interface

Just try it out:

Demos

Tagify with default settings

Type in your tags separated by comma. Remove them with a click on the X-Button or use backspace.


show sourcecode

Filled with some initial tags and with automatic detection of duplicates + Effects

Type in your tags separated by comma:


show sourcecode

Tagify with other separators and a default value

Type in your tags separated by comma, semicolon or space. The Tags that are already visible are automatically parsed out of the input's value attribute.


show sourcecode

Installation

Tagfiy requires the JavaScript framework Prototype. If you want to use effects you also need script.aculo.us. You can grab it here and here or include it directly via the Google AJAX Libraries API. Tagify uses CSS for styling the input element for your tags so you need to include the css-file which is included in the download. Upload the image for the remove button. All you have to do now is to include the JavaScript and CSS files. If you have other paths or want other images: check the image paths in the tagify.css file.

It's as simple as this:

<script type='text/javascript' src='http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js'></script>
<script type='text/javascript' src='/tagify/tagify.js'></script>
<link rel="stylesheet" type="text/css" href="/tagify/tagify.css" />

If you don't want to load an exta Stylesheet you could simply copy the contents of /tagify/tagify.css to your default css-file.

How to use Tagify

Usage is really simple. If you use the defaults provided by Tagify all you have to do is this simple call:

new Tagify('id_of_your_input_element');

Tagify options and API

Tagify has some options, they are all listed below with their defaul values:

new Tagify('id', {
	splitBy: ",", // String of seperators for tags
	duplicates: true, // allow or disallow duplicates
	strip: true, // strip/trim text
	parameterName: false, // name-attribute of the generated hidden input-elements
	className: "tagify", // css-class name
	add:[], // default tags
	removeEffect: false, //effects and effect-options
	addEffect:false,
	removeEffectOptions: {},
	addEffectOptions: {},
	duplicateEffect: Effect.Highlight
}
});

First Parameter: Element

The first parameter of Tagify is either an input element or the id of an input element. The given element will be converted to a Tagify-Box.

Second Parameter: Options

splitBy

With this option you can specify the seperators for your text. By default tags are seperated by ",". You can however add as many seperators as you like:

new Tagify('id', {splitBy: ', ;'});

In this example tags can be seperated by comma, semicolon or space.

Because this separators are used in a regular expression you have to escape charactars with a special meaning... (This should however not be a problem for you in most cases...)

duplicates

If you set duplicates to true Tagify does not add one tag with the same text twice.
new Tagify('id', {duplicates: false});

strip

By default Tagify removes whitespaces before and after a tag. If you don't want to remove this blanks set strip to false.
new Tagify('id', {strip: false});

parameterName

With this option you can customize with wich parameter the data should be send to your server. If you leave out this paramter Tagify will automatically detect the name of your original input and append "_tagify" to it. The data then gets send as an array with the specified name.

// change the name to "tags"
new Tagify('id', {parameterName: "tags"});

// default behaviour is this:
//HTML < type="text" name="words" />
new Tagify('id');
// Tags will be available on the server side in an array called words_tagified

Handling on the server side
Here is a commented PHP example:
//if your form is a GET instead of a POST just replace POST with GET.
// if JavaScript is disabled Tagify does not work and there will be no words_tagified:
if(!isset($_POST['words_tagified'])
{
	//this is just a fallback if JS is disabled
	//retrieve the tags from the normal string, and explode it:
	$tags = explode(",", $_POST['word']);
}
else
{
	$tags = $_POST['words_tagified'];
}

foreach($tags as $tag)
{
	//save to database or echo them....
	echo $tag;
}
	  

className

Defaults to "tagify". You can change it to whatever you want. className is the name/prefix that is used for Tagify's css-classes.
new Tagify('id', {className: "tagified"});
If you change this value make sure to also change the CSS file.

add

Defaults to an empty array. All elements of the add-array are automatically addes as initial tags:
new Tagify('id', {add: ["dog", "cat", "mouse"]});
Instead of using the add-option you can also set the value attribute of your tagified input:
<input type="text" name="words" value="dog,cat,mouse" />

addEffect

If you include Script.aculo.us you can trigger effects on certain events. With addEffect you can fire a effect on a newly added tag. It works like this:
new Tagify('id', {addEffect: Effect.Appear});
To customize this effect you can pass in a hash like this:
new Tagify('id', {addEffect: Effect.Appear, addEffectOptions: {duration:0.5, from:0.1}});
For futher documentation check the Script.aculo.us documentation.

removeEffect

This effect is used on the element/tag that is removed
new Tagify('id', {removeEffect: Effect.Fade});
To customize this effect you can pass in a hash.
new Tagify('id', {removeEffect: Effect.Fade, removeEffectOptions: {duration:0.5}});

duplicateEffect

If you disable duplicates in Tagify and a user wants to add a tag that is already added you can use this effect to highlight the duplicate:
new Tagify('id', {duplicateEffect: Effect.Highlight});
To customize this effect you can pass in a hash.
new Tagify('id', {duplicateEffect: Effect.Highlight, duplicateEffectOptions: {duration:0.5}});

API

Tagify has one public method:

add()

At anytime it is possible to add tags manually like this:
var tf = new Tagify($('element'));
tf.add('mytag');

Cusomize design

To customize the design just look into the tagify.css and change it to whatever you like.

License

Just grab it and use it...free.

Contact

If you need help or have features requests feel free to comment to this blog post: Tagify - a tagging interface.