Question

I'm experimenting with jQuery tokeniputs and would like to get not only the item.name but also the id of the input when a new item gets added. There is a function variable onAdd that i use to invoke a alert:

onAdd: function (item) {
    alert("Added " + item.name);
},

But i really have no clue how i should get the id of the input the element gets appended to:

<input type="text" id="input12" name="blah" />

My demo code:

$("#input12").tokenInput("http://shell.loopj.com/tokeninput/tvshows.php", {
    prePopulate: [{
        id: 123,
        name: "Slurms MacKenzie"
    }],
    onAdd: function (item) {
        alert("Added " + item.name);
    },
    onDelete: function (item) {
        alert("Deleted " + item.name);
    }
});

http://jsfiddle.net/v394w/

Thanks!

Était-ce utile?

La solution

If you want the ID of the input you can do this this.attr("id"):

$(document).ready(function () {
    $("#demo-input-pre-populated").tokenInput("http://shell.loopj.com/tokeninput/tvshows.php", {
        prePopulate: [{
            id: 123,
            name: "Slurms MacKenzie"
        }, {
            id: 555,
            name: "Bob Hoskins"
        }, {
            id: 9000,
            name: "Kriss Akabusi"
        }],
        onAdd: function (item) {
            console.log(this.attr("id"));
            // Input ID (this refers to the jQuery input object)
        },
    });
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top