hailwood / jQuery-Tagit

The Jquery Tagit Plugin transforms an html unordered list into a unique tagging plugin.
264 stars 109 forks source link

problem with $(this).text() #15

Closed Zhouxin2 closed 12 years ago

Zhouxin2 commented 13 years ago

Hello

I'm going to set tag by click functions, but I had a problem with initial tags. When i use this code :

$(".tag-a").click(function(e) {
    e.preventDefault();
    var tags = $('#ul_tags_input').tagit('tags');
    var tag = $(this).find("span").html();
    if(tags.indexOf(tag)<0){
        tags.push(tag);
    }
    $("#ul_tags_input").tagit("destroy");
    $("#ul_tags_input").tagit({initialTags:tags});
});

It looks like every tag get an extra "x" after $("#tags").tagit({initialTags:tags}) I think it's because of: $(this).text() get a x from x of

  • probability i'm wrong...

    When I changed your code to

    self.options.initialTags.push(
        tagValue ? {label: $(this).text().slice(0,-1), value: tagValue} : $(this).text().slice(0,-1)
    );

    works fine...

    Am i doing the right way? Please let me know if there's a better way to achieve this.

    Sorry for my poor English...

  • hailwood commented 13 years ago

    Hi Zhouxin2, Cheers for using my plugin.

    I have edited the version on GitHub to add a function called add(label, value) which will allow you to call it the same as you call any function on a jquery UI plugin to add a tag.

    Please download and try this for me as I have not had the time to test.

    Zhouxin2 commented 13 years ago

    It works. Thank you very much!