How can i call a function inside onChange of selectize?
Asked Answered
K

2

8

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

Kirkpatrick answered 1/7, 2015 at 8:33 Comment(0)
G
13

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);
}
Gnathonic answered 6/7, 2015 at 17:3 Comment(1)
Thank you, silly me, i was using weatherWidget[value]; :PKirkpatrick
M
2

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);
                }
                
            }
        });
Mullah answered 17/10, 2020 at 18:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.