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

Chaning the value programmatically doesn't work #181

Closed
kobelobster opened this issue Oct 21, 2015 · 8 comments
Closed

Chaning the value programmatically doesn't work #181

kobelobster opened this issue Oct 21, 2015 · 8 comments
Labels

Comments

@kobelobster
Copy link

In the onInit method I'm calling the following peace of code

$slider.parent().find('input[type=range]').val($sliderVal['slider']).change();

However, the ranger is not chaning it's value.

/// This returns 1, so it should work
$slider.parent().find('input[type=range]').length 
// This returns 20 (not "20", simply 20)
console.log($sliderVal['slider']);
@andreruffert
Copy link
Owner

Hi @tzfrs! First of all what are you trying to achieve? It's not clear to me so far...

@kobelobster
Copy link
Author

On your website you have this piece of code

image

I guessed this would be the code snippet I have to use to achieve this

http://andreruffert.github.io/rangeslider.js/#js-example-change-value

As you can see, I'm selecting the input with the type range, setting a value, and calling the change method. Shouldn't it change now programmatically? Or is it the wrong method I'm using?

If it is the wrong one I have a suggestion for your website: Would be cool if you have the code next to the examples for easier copying and less confusion.

@andreruffert
Copy link
Owner

K let's say you have a situation like this:

var $myElement = $('#myElement');

// initialize a rangeslider.js instance
$myElement.rangeslider({...});

and now you want to change the value:

$myElement.val(10).change();

// => should be `10` now
console.log($myElement[0].value);

I guess the problem is this:

In the onInit method I'm calling the following peace of code
$slider.parent().find('input[type=range]').val($sliderVal['slider']).change();

If you change the value of an element which gets initialized at the moment there could be some race conditions...

Why are you changing the value on initialization? Is there any reason for that?
I would set the value right before! Directly in the markup or with JS unless it's not possible otherwise.

@andreruffert
Copy link
Owner

Closing because of no feedback...

@kobelobster
Copy link
Author

Sorry for the late answer!

The reason I want to change the value at initialization is the following: A user uses the slider and submits a form, then he comes back to the site. His choice however is saved in the session. Currently I'm reading this session and setting the value of the slider to the value he selected, however I thought there would be a way to do that with the rangeslider directly

@andreruffert
Copy link
Owner

The reason I want to change the value at initialization is the following: A user uses the slider and submits a form, then he comes back to the site. His choice however is saved in the session. Currently I'm reading this session and setting the value of the slider to the value he selected, however I thought there would be a way to do that with the rangeslider directly

There is a way of doing it directly with rangeslider.js but I would recommend to set the value from the session directly to the input e.g.

<input type="range" value="{{sessionValue}}">

@kobelobster
Copy link
Author

Yep, that's what I'm currently doing. Was just wondering how it would work in a JS way. But I'm using the PHP solution now. Thanks anyway

@andreruffert
Copy link
Owner

Ok kewl you're welcome! ✌️

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

No branches or pull requests

2 participants