value not available in create event on jQuery UI slider?
Asked Answered
A

3

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?

Accumbent answered 30/4, 2012 at 18:4 Comment(3)
The above code is not sufficient to say why. How is create being invoked?Kovacs
where is create defined... Please post the full sourceRamshackle
This is a built-in callback in jquery-ui. It is well-documented. I don't think he should have to post the source.Aquarist
B
9

You can also use

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

DEMO.

Baguio answered 30/4, 2012 at 18:34 Comment(3)
Thanks. I'm still confused on why ui.value doesn't work, but this works for me.Accumbent
You are most welcome :-) described here jqueryui.com/demos/slider in methods section.Baguio
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');Pyrrolidine
M
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.

Murdock answered 30/4, 2012 at 18:20 Comment(1)
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).Billowy
S
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

Sudoriferous answered 13/2, 2019 at 18:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.