0

How to set value of newly added option at run time. Currently taking option name in value. I am using below code.

 $(document).ready(function () { 
         $('#dept').selectize({  
            create: true, sortField: 'text' 
        });
}); 

enter image description here

SilverSurfer
  • 4,281
  • 6
  • 24
  • 50
Pankaj
  • 169
  • 2
  • 11

2 Answers2

0

TO add new option and customize its value use createItem and updateOption methods available in Selectize API. Check this fiddle.

Nidhin Chandran
  • 846
  • 8
  • 19
0
Its working for me like
$(function(){
        $('#desig').selectize({
            create:function (input, callback){
                $.ajax({
                    url: "<?php  echo $this->url(null, array('controller' => 'employee-detail', 'action' => 'add-new-desig-ajax'));?>",
                    type: 'POST',
                    data:{'designation':input},
                    success: function (result) {
                        if (result) {
//                            console.log('sdfasf',result);
                            callback({ value: result.id, text: input });
                        }
                    }
                });
            }
        });
    }); 

I have taken help from here
https://stackoverflow.com/questions/24366365/creating-an-item-if-not-already-exists-in-selectize-js-select-box-and-ajax-updat/46096111#46096111
Pankaj
  • 169
  • 2
  • 11