Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to move the slider programmatically? #29

Open
gupta-ankit opened this issue Feb 25, 2015 · 3 comments
Open

Is it possible to move the slider programmatically? #29

gupta-ankit opened this issue Feb 25, 2015 · 3 comments

Comments

@gupta-ankit
Copy link

Currently I can create a slider like this

var slider = d3.slider().min(0).max(10);
d3.select("#slider").call(slider)

This creates a slider. Is there a way to update the position of the slider via javascript. For example, executing slider.value(5) should update the position of the slider.

@wahlatlas
Copy link

Yes, it is (basically your syntax, maybe naming the variable "slider" is an issue?). This works for me:

var mySlider = d3.slider()
.min(0)
.max(10)
.value(7)
.on("slide", function(evt, value) {
    doSomething(value)
}); 

d3.select("#div-where-the-slider-will-be-attached-to")
.call(mySlider);

mySlider.value(newValue);

Do you work with the latest version of the slider? This functionality became available on 29 Sep 2014 through bb33d87

@VD17593
Copy link

VD17593 commented Nov 8, 2015

I experienced also a problem with the getter/setter function for the value.
When the value is not changed at creation (as in the example above), or when the value is set to 0, the setter doesn't work.
The problem (I think) in the code below is that the condition if (value) { will return false if the value is zero.

slider.value = function(_) {
  if (!arguments.length) return value;
  if (value) {
    moveHandle(stepValue(_));
  };
  value = _;
  return slider;
};

Therefore I would suggest to use another condition like:

if (typeof value !== 'undefined') {

or

if (value>=0) {

Regards.
Nice plugin by the way.

@citrusvanilla
Copy link

man thank the lord i found this @VD17593 thanks for the spot.. confirmed same behavior for me trying to set a value to something other than zero... new to JS so a rude way to find out that 0 == false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants