jquerytokenize2

how do i get text of multiselect tokenize2 control


How do i get Text of multiselect tokenize2 control? i can easily get value of multi select but i dont know how to fetch text

Plugin Link : https://zellerda.github.io/Tokenize2/index.html

Code:

<select class="tokenize-demo" multiple>
  <option value="1">Africa</option>
  <option value="2">Americas</option>
  <option value="3">Asia</option>
  <option value="4">Europe</option>
  <option value="5">Oceania</option>
</select>

Jquery:

<script>
  $('.tokenize-demo').tokenize2();
    $('.tokenize-demo').on("tokenize:tokens:add", function (event, value) {
    console.log("Value is "+value); //here i am getting selected value like 1,2,3..etc.
    console.log("Text is "+?);//here i want text from options like Africa
  });
</script>

Solution

  • There is not enough documentation but I managed to found the solution. Text propert is actually passed in the third parameter of event listener function. Modify your event listener as below:

    $('.tokenize-demo').on("tokenize:tokens:add", function (event, value, text){
         console.log("Value is "+value); // To get value
         console.log("Text is "+ text); // To get text
    });
    

    For reference please see tokenize:tokens:add event documentation