Tag it! Tag suggestions, editor and autocomplete in a jQuery UI plugin

After looking for a jQuery plugin for handling a tag suggestion form field, also with a autocomplete feature – in much the same way ZenDesk.com does – I ended up developing a customization of jQuery UI that does the same interaction.

UPDATE June 19th, 2011: due to other pressing priorities I delegated the development/support of this plugin to Alex Ehlke who is now doing an amazing job keeping this plugin up to date. Links below are now pointing to his repository. Thanks for your amazing help, Alex!

Official page for the project: http://aehlke.github.com/tag-it/

See the screenshot

See the Demo

Fork Tag it! on Github

Download the Tag it! plugin

Cheers! ;)

Share and Enjoy:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google

Tags: , , ,

27 Responses to “Tag it! Tag suggestions, editor and autocomplete in a jQuery UI plugin”

  1. Chalet16 Says:

    What license is this plugin?

  2. James Says:

    I got it working well enough in FF, but it does not seem to work in IE 8. The box is not even visible.

    Also, the css affects other elements on the page (pretty much all of them).

  3. Bulan Says:

    Hi!

    Tried your plugin, quite nice, butI have a few questions.

    1. How do I set some initial tags ?
    I have a page with saved tags, I want to list them in the editor if I reload the page.

    2. Is there a option to only match the first letters of the autocomplete?
    If I type “av”, i get the suggestions; “java” & “javascript”.
    I would like to only get them if it matches the beginning of the word (“ja”)

  4. Bulan Says:

    Figured out the answer to question 1 myself and also fixed if empty…

    Added this to your script:

    //… row 4
    var defaults = {
    availableTags: [],
    values: []
    }

    var options = $.extend(defaults, options);

    //… row 26
    // read initial values
    for (var i = 0; i < options.values.length; i++) {
    create_choice(jQuery.trim(options.values[i]));
    }

    //——————-

    Calling Tag It! with:

    $(“#mytags”).tagit({
    availableTags: ["tag1","tag2", "tag3"],
    values: ["tag2"]
    });

    Still wondering about Q2 though.. :)

  5. James Says:

    BTW, IE didn’t like the ‘const’ declarations. Changed them to ‘var’ and it works.

  6. Maurice Says:

    Hi,

    Great script! I am trying to have it show the previously added tags for an item. So after saving them in the DB, i can edit/delete of add tags. But can’t get it to work. Any ideas? For now I’ve tried to create a loop and add this for each DB result:

    while($arr=mysql_fetch_array($res)) {

    $tagitems.=’
    ‘.$arr['tag_name'].’
    x

    ‘;
    }

    Any ideas?

  7. Stefano Says:

    Hi all,
    i’m testing the tag-it plugin ( congratulations, very very nice and useful).

    i’ve a question: i have a web form with tagit! and by php i send the choosen values to mysql. (the single tags are picked up by the “insert.php” script and imploded into a string to the database). here everything goes well.

    Then i have the necessity to re-open the form, pick up the saved tags to visualize them (explode the string of the db to a new array), and of course add new tags or delete a previous one.

    i thought this could work, but nothing….

    foreach ($array as $tags)
    {
    echo’ ‘.$tags’.x
    ‘;
    }

    also adding the final string:

    nothing… no-one of the loaded value is displayed and the script just show me an emty field.

    Do you think what i’m trying to realize would be possible?

    Thank you very much if you can help me to resolve this nightmare !! :-)
    stefano

  8. Rich Allen Says:

    Was searching for somthing like this and this looks perfect, great job but I have a question about implementation. the auto complete is working but in my implementation instead of creating new tags inside the box, I get a new line each time (screen shot: http://img2.pict.com/fe/79/04/3594365/0/1275495856.jpg). Is this css or have I implemented it incorrectly:

    $(document).ready(function(){
    $(“#tags”).tagit({
    availableTags: ["fish", "turtles", "janurary jones", "ocean"]
    });
    });

    ..
    ..
    ..
    Tags:

  9. Rich Allen Says:

    Sorry about the bad post! didn’t realize that it would take the html tag and try to use them. I put it up in pastebin for review =) http://pastebin.com/Kqv96y3Q

  10. Rich Allen Says:

    I’m trying to work to fast here and missing simple things. Haha fixed so ignore my dumb questions and thanks again for the plugin!

  11. Raphaël Says:

    Hi,

    I tried to put 2 inputs, but there is some bug. The last input is always focus when I click on other input.

    Try this :
    Tags

    Tags

    JS :
    $(“#mytags”).tagit({
    availableTags: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "perl"],
    values: ["tag2"]
    });

    $(“#mytags2″).tagit({
    availableTags: ["c++", "java", "php", "coldfusion", "javascript", "asp", "ruby", "python", "c", "scala", "groovy", "haskell", "perl"],
    values: ["tag2"]
    });

    I think the problem is near from :
    // add the tagit CSS class.
    el.addClass(“tagit”);

    // create the input field.
    var html_input_field = “\n”;
    el.html (html_input_field);

    tag_input = el.children(“.tagit-new”).children(“.tagit-input”);

    but I’m not an expert in JS.

    Someone could help me plz ?

    Thanks

  12. peppe Says:

    if i want to add 1000 tags, can i open the list from an external file like txt without include in the javascript all the tags?
    sorry for my english

  13. chris Says:

    Changing…

    function split(val) {
    return val.split(/,\s*/);
    }

    function extractLast(term) {
    return split(term).pop();
    }

    tag_input.autocomplete({
    source: function(request, response) {
    $.getJSON(“search.php”, {
    term: extractLast(request.term)
    }, response);
    },

    And works great for my needs ;) thx alot

  14. saric Says:

    Hi there,
    the plugin looks very good. Is there a possibility to use tagit! plugin for tags, which are stored in DB (using Ajax).

    Thanks in advance for answers.

  15. Adam Says:

    Bulan you are the man!

  16. Mazil Says:

    How to use multiple tag it fields on the same page…

    (Line 19)
    Change: tag_input = el.children(“.tagit-new”).children(“.tagit-input”);
    To: var tag_input = el.children(“.tagit-new”).children(“.tagit-input”);

    Then change all references to “this.tag_input” to “tag_input”.

  17. Oakstair Says:

    Is there a way to prevent users from creating new tags. I.e. we have some forms where they should only be allowed to choose among existing tags.

  18. Frank Says:

    I like this plugin very much, please chance the const to var like James said.

    Is it possible to print a list with all the tags beneath it where they can select from?

    Cheers

  19. Double Headboard  Says:

    the best thing about IE8 is that it is quite stable than previous releases of Internet Explorer”";

  20. PNN Says:

    Hello
    Can u help me get list tag from database ?

  21. Andy Says:

    Great work! Just what I was looking for! Any chance to make it work with an ajax call to a .php file instead of an array of predefined words?
    Thanks!

  22. toni Says:

    hey there! i can’t get it work but it exactly what i need :)

    this is my code:

    $(document).ready(function(){
    $(“#ingredientes”).tagit({
    availableTags: [ 'ajo','cebolla','aceite','patatas','mantequilla','sal','azucar','coñac','pan','agua-gas','caldo','arroz-blanco','perejil','harina','pimienta','albahaca','harina-fuerza','salsa-soja','pimiento-rojo','congrio','almejas','vino-blanco','pan-tostado','nata','manteca-sesamo','levadura-fresca','hierbabuena','pan-seco','merluza','alubias-verdes','huevo','queso-rallado','huevos','pasta','lima','agua','avecrem','rape','zanahoria','lechuga','queso','tomate','ron','pimenton','aceite-oliva','gambas','acelgas','caldo-pescado','puerros' ]
    });

    });

    Where ingredientes it’s the id of the input. firebug gives me no error and tag-it.js it’s imported… any idea? :S

    thanks for developing this awesome plugin i can’t wait to get it working

  23. Wolkam Says:

    I have te same problem than Raphael, I put two tagit field, and when i click on the first, the second fiel take the focus. I did tha Mazil’s modification, but it bug again.
    If someone find the solution, It will help me a lot.

    Thanks

  24. Wolkam Says:

    I found the problem, the tags fields must be in separate DIV, and it works

    Tags

    Tags 2

    TY all for this script

  25. Wolkam Says:

    Sorry the previous post don’t show the html tag

    <div class="line">
    <label for="ingredient_list">Tags</label>
    <ul id="mytags"></ul>
    </div>
    <div class="line">
    <label for="ingredient_list2">Tags 2</label>
    <ul id="mytags2"></ul>
    </div>
    <input type="submit" value="OK">

  26. the-FoX Says:

    ie doesn’t support const in javascript as far as i know.
    just replace const by var

  27. Alex Ehlke Says:

    Hi,

    I updated this plugin to support a single input field for holding all the form data for all the tags, rather than one hidden input per tag. This is an optional feature — see the docs and example usage.

    Here is the git repo with the updates: https://github.com/aehlke/tag-it

    Thanks for your nice work!