5
$('#foo').slider({
  range: 'min',
  min: 0,
  max: 1000,
  step: 100,
  value: 500,
  create: function( event, ui) {
          var bar = ui.value;
  },
  //etc...
});

Why is bar undefined and not 500? Is it possible to assign a variable to the value in the create event?

jaacob
  • 471
  • 5
  • 13
  • 1
    The above code is not sufficient to say why. How is `create` being invoked? – Tejs Apr 30 '12 at 18:08
  • where is create defined... Please post the full source –  Apr 30 '12 at 18:12
  • 2
    This is a built-in callback in jquery-ui. It is well-documented. I don't think he should have to post the source. – Jeff B Apr 30 '12 at 18:23

3 Answers3

9

You can also use

create: function( e, ui ) {
    var bar=$(this).slider('value');
}

DEMO.

The Alpha
  • 143,660
  • 29
  • 287
  • 307
  • 1
    Thanks. I'm still confused on why `ui.value` doesn't work, but this works for me. – jaacob Apr 30 '12 at 19:09
  • You are most welcome :-) described here http://jqueryui.com/demos/slider/ in `methods` section. – The Alpha Apr 30 '12 at 19:12
  • For some reason the create callback was not invoked in my case, so I had to bind the event maunaly before creating slider $('.slider').on('create', function(event, ui){...}); and then trigger the create event manually too after creating slider like, $('.slider').slider().trigger('create'); – Simon Polak Sep 13 '14 at 12:46
1

I'm not really sure why you'd need to get the value when you're instantiating the slider since you're setting it anyway, but you can do it this way:

$('#foo').slider({
  range: 'min',
  min: 0,
  max: 1000,
  step: 100,
  value: 500,
  create: function( event, ui) {
          var bar = $('#foo').slider("value");
  },
  //etc...
});​

jsFiddle example.

j08691
  • 204,283
  • 31
  • 260
  • 272
  • You might need that value if for example you're using a permanent tooltip on the handle to show the current value. Ideally you would only set the value for the slider and instantiate the tooltip once the slider is created (i.e. using the "create" callback). – radu.luchian Feb 16 '15 at 09:31
0

The ui parameter for create method is empty as per their API documentation hence var bar = ui.value is undefined just in case anyone wondered why it did not work.

Please see reference

Vinu
  • 89
  • 5