Search code examples
twitter-bootstrapbootstrap-tokenfield

Twitter bootstrap tokenfield


I cannot get the create token or set token to function. Im using following:

$('#myField').tokenfield('createToken', { value: 'violet', label: 'Violet' });
$('#myField').tokenfield('setTokens', ['blue','red','white']);

But its not working. Is there a new way to do it or what can be wrong?

Html:

<div style="width: 400px; margin-left: 20px;">
    <input id="myField" type="text" width="200" />
</div>

Solution

  • Having looked through the documentation, it doesn't scream as a pre-requisite! But you'll have have initialise it before calling .tokenfield() functions, as per your code below:

    $('#myField').tokenfield('createToken', { value: 'violet', label: 'Violet' });
    $('#myField').tokenfield('setTokens', 'blue,red,white');
    

    Demo Fiddle: http://jsfiddle.net/fLjVt/

    Add $('#myField').tokenfield(); above the two functions you've added from the docs itself:

    /**
     *  Initializes an input with a tokenfield.
    **/
    $('#myField').tokenfield();
    
    /**
     *  Populate the tokenfield()
    **/
    $('#myField').tokenfield('createToken', { value: 'violet', label: 'Violet' });
    $('#myField').tokenfield('setTokens', 'blue,red,white');
    

    Demo Fiddle: http://jsfiddle.net/myQf7/