/    Sign up×
Community /Pin to ProfileBookmark

Jquery – slider and input box – update each others’ values

Hi,

I’ve got a slider in jquery and I want it to populate an input box with a value as it is slid. I also want the user to be able to enter a value into the input box and for the slider handle to move to the appropriate position.

I’ve found an example of the bare-bones slider in the jquery docs and some basic documentation, but I’ve no idea how to get it to do what I need, and can’t find any simple, fully featured examples anywhere which do what I need.

Can anyone suggest how to modify the following code to get me started?

[code]<script type=”text/javascript”>
$(document).ready(function(){
$(“#example”).slider();
});
</script>

<div id=”example” class=”ui-slider-1″ style=”margin:10px;”>
<div class=”ui-slider-handle”></div>
</div>
<input type=”text” class=”slider-input” id=”slider-input” />
[/code]

Thanks folks…

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@danwednesdayauthorMar 30.2008 — Yay! Done part of it, but I can't seem to get the input element to change the slider handle location onblur. I can get the slider to change the value in the input box but not the other way round. It doesn't error, it just makes the handle go to zero on the slider on blur:

&lt;script type="text/javascript"&gt;
var slider1Value = 4;
var slider1Stepping = 1;
var slider1Min = 1;
var slider1Max = 2500;

<i> </i>$(document).ready(function(){
<i> </i> $("#example").slider({
<i> </i> 'stepping': slider1Stepping,
<i> </i> 'minValue': slider1Min,
<i> </i> 'maxValue': slider1Max,
<i> </i> 'startValue': [slider1Value],
<i> </i> 'slide': function(e, ui){
<i> </i> $('.slider-input').val(ui.value);
<i> </i> }
<i> </i> });
<i> </i> $('.slider-input').blur(function(){
<i> </i> var slider1Value = $(".slider-input").attr("value");
<i> </i> if(slider1Value &gt;= slider1Min &amp;&amp; slider1Value &lt;= slider1Max){
<i> </i> $('#example').slider('moveTo',slider1Value);
<i> </i> }else{
<i> </i> alert('Please enter a value between '+slider1Min+' and '+slider1Max);
<i> </i> return false;
<i> </i> }
<i> </i> });
<i> </i>});
&lt;/script&gt;


Can anyone see why this might be, and how to fix it?

Thanks
Copy linkTweet thisAlerts:
@Orc_ScorcherMar 31.2008 — You have to convert the input's value from a string to a number first:<i>
</i> var slider1Value = [color=green]+this.value[/color]
Copy linkTweet thisAlerts:
@danwednesdayauthorMar 31.2008 — Hmm, that's not working:

&lt;script type="text/javascript"&gt;
var slider1Value = 4;
var slider1Stepping = 1;
var slider1Min = 1;
var slider1Max = 2500;

<i> </i>$(document).ready(function(){
<i> </i> $("#example").slider({
<i> </i> 'stepping': slider1Stepping,
<i> </i> 'minValue': slider1Min,
<i> </i> 'maxValue': slider1Max,
<i> </i> 'startValue': [slider1Value],
<i> </i> 'slide': function(e, ui){
<i> </i> $('.slider-input').val(ui.value);
<i> </i> }
<i> </i> });

<i> </i> $(".slider-input").attr("value",slider1Value);

<i> </i> $('.slider-input').blur(function(){
<i> </i> var slider1Value = +this.value;
<i> </i> if(slider1Value &gt;= slider1Min &amp;&amp; slider1Value &lt;= slider1Max){
<i> </i> $('#example').slider('moveTo',slider1Value);
<i> </i> }else{
<i> </i> alert('Please enter a value between '+slider1Min+' and '+slider1Max);
<i> </i> return false;
<i> </i> }
<i> </i> });
<i> </i>});
&lt;/script&gt;


Can you spot the problem?

Thanks Orc
Copy linkTweet thisAlerts:
@Orc_ScorcherMar 31.2008 — That works for me, but my HTML markup is probably different from yours. Do you have a test page online?

Btw, you should better use the 'change' callback instead of the 'slide' callback or your script won't be notified when the user changes the value of the slider by clicking on it.
Copy linkTweet thisAlerts:
@danwednesdayauthorMar 31.2008 — A demo is here: http://www.iomi.net/test-slider.htm.

The 'slide' callback needs to be like that really so that the user has feedback on what value they're sliding to. I tried changing it to 'change' but that then doesn't update the input box when you're sliding. For the UI it definitely needs to be the former.

Thanks Orc
Copy linkTweet thisAlerts:
@Orc_ScorcherMar 31.2008 — Looks like you've got a different version of ui.slider.js. I'm using the one from this archive:

http://code.google.com/p/jqueryjs/downloads/detail?name=jquery.ui-1.5b.zip

Keep in mind jQuery UI is still beta software and some versions may work better than others.
Copy linkTweet thisAlerts:
@danwednesdayauthorMar 31.2008 — Yep, dodgy version of jquery UI. All sorted. Thanks again for your help...
Copy linkTweet thisAlerts:
@ynwApr 02.2008 — Looks like you've got a different version of ui.slider.js. I'm using the one from this archive:

http://code.google.com/p/jqueryjs/downloads/detail?name=jquery.ui-1.5b.zip

Keep in mind jQuery UI is still beta software and some versions may work better than others.[/QUOTE]


Thank Orc Scorcher invaluable info. Looking into the generally avaible implementation its just... wrong ?


---

Ruby on Rails tutorial
Copy linkTweet thisAlerts:
@bgnm2000Mar 23.2009 — hey guys!

this worked really well for me.

My only question is, my form field may already contain a value. Is there a way to make that value the starting value for the slider handle?
Copy linkTweet thisAlerts:
@bgnm2000Mar 24.2009 — hey guys!

this worked really well for me.

My only question is, my form field may already contain a value. Is there a way to make that value the starting value for the slider handle?[/QUOTE]



I know this must be possible - I'm just too big of a javascript newb to figure it out. Anyone have any ideas?
Copy linkTweet thisAlerts:
@shyamaliNov 22.2011 — The code works like superb !!!!!!! But is it possible to input a value in texbox and the slider will move accordingly?
×

Success!

Help @danwednesday spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.26,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...