I want to create a custom slider plugin in Widget Factory, but it doesn't work. Why can't I use either the .option() function, or the .setMyOptions() custom function I created. I get the TypeError: <function> is not a function error.
What am I doing wrong? I know this is some simple syntax tweak and has to do with scopes, but I don't know what exactly is wrong.
$.widget("custom.niceSlider", {
options: {
value:0
},
myOptions: {
handleWidth:'30px'
},
_create: function(){
this.element.addClass("niceSlider");
this._bar = $("<div class='bar' id='bar'></div>")
.appendTo(this.element);
this._handle = $("<div class='handle' id='handle'></div>")
.appendTo(this._bar)
.css("width", this.myOptions.handleWidth );
},
_destroy: function(){
},
_setOption: function( key, value ){
},
setMyOptions: function(key, value){
switch(key){
case 'handleWidth':
this.myOptions.handleWidth = value;
break;
}
}
});
$(document).ready(function(){
var slider = $("#slider").niceSlider();
slider.setMyOptions('handleWidth','50px');
});
You will have a fair amount of work ahead of you to build your own slider widget. I would advise looking at just extending
$.ui.slidera bit.To address your question, I would examine this code:
Working Example: https://jsfiddle.net/Twisty/q6e7t3pn/2/
First, I noticed you had assigned IDs that would not be unique. This is fine for 1 slider, but not for more than 1. Other than that, there was nothing major wrong with the code. It was more about how you called it. There are three ways to do that:
Or:
Or:
From: Widget Method Invocation
Update
You may want to consider a simplier solution:
Example: https://jsfiddle.net/Twisty/q6e7t3pn/4/