Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i call a function inside onChange of selectize?

Tags:

selectize.js

How can i call a function inside onChange of selectize?

onChange: function(value){
  weatherWidget(value)
}

$(document).ready(function() {
  function weatherWidget(location){
    console.log('test');
  }
}

this code gives me not defined function. Thank you

like image 763
user3779015 Avatar asked Jul 01 '15 08:07

user3779015


2 Answers

You have a scope issue. You can either wrap your selectize call in $(document).ready as well (which it should probably be wrapped in regardless) or just leave your function declaration outside of it, as it will be loaded in parse time. I recommend leaving the function outside of $(document).ready, like so:

$(document).ready(function() {
  $(x).selectize({
    ....
    },
    onChange: function(value) {
      weatherWidget(value);
    }
  });    
});

function weatherWidget(location) {
  console.log(location);
}
like image 164
basher Avatar answered Dec 22 '22 02:12

basher


This is how you can trigger value change in selectize dropdown list:

       $('#selector').selectize({
            onInitialize: function() {
                this.trigger('change', this.getValue(), true);
            },
            onChange: function(value, isOnInitialize) {
                if(value !== ''){
                   alert('Value has been changed: ' + value);
                }
                
            }
        });
like image 21
Sharhabeel Hamdan Avatar answered Dec 22 '22 02:12

Sharhabeel Hamdan