Search code examples
javascriptjquerytag-it

Tag it submit id not value or label


using this plugin https://github.com/aehlke/tag-it its very cool by the way.

problem:

        <input type="hidden" name="tags" id="mySingleField" value="Apple, Orange" disabled="true">
        Tags:<br>
        <ul id="mytags"></ul>

<script type="text/javascript">
    $(document).ready(function () {
        $("#mytags").tagit({
            singleField: true,
            singleFieldNode: $('#mySingleField'),
            allowSpaces: true,
            minLength: 2,
            removeConfirmation: true,
            tagSource: function (request, response) {
                //console.log("1");
                $.ajax({
                    url: "../City/GetList",
                    data: { term: request.term },
                    dataType: "json",
                    success: function (data) {
                        response($.map(data, function (item) {
                            return {
                                label: item.label + " (" + item.id + ")",
                                value: item.value
                            }
                        }));
                    }
                });
            }
        });
    });



</script>

When tag it selects the values it adds values to the hidden field in CSV format in value attr. i want it to do ID instead anyone know how to ?


Solution

  • Change the tag-it.js file

    Comment from line 264

    // that.createTag(that._cleanedInput());
    
    // The autocomplete doesn't close automatically when TAB is pressed.
    // So let's ensure that it closes.
    // that.tagInput.autocomplete('close');
    

    around line 285

    var autocompleteOptions = {
        select: function(event, ui) {
            that.createTag(ui.item);                        
    

    Create a new function

    assignedTagsData: function(){
        // Only to be used when singleField option is not seleted
        var tags = [];
        this.tagList.children('.tagit-choice').each(function() {
            tags.push($(this).data('tag_item_data') );
        });
        return tags;
    }
    
    that.createTag(ui.item);
        
    

    Create tag

    var tag = $('<li></li>')
        .data('tag_item_data',item) //add this line
        .addClass('tagit-choice ui-widget-content ui-state-default ui-corner-all')
        .addClass(additionalClass)
        .append(label);