How to display the value of jQuery UI Slider in a text input and update the Slider if a value is entered in the text input?

Asked

Viewed 981 times

2

The result I need to achieve using jQuery UI Slider seems to be simple. The Slider will be used to define the age of the user who would submit the form, with minimum value 18 (years) and maximum 100 (years)... The intended purpose can be divided into two parts:

First part > Show the Slider value in a text input:

So far, I can make the first part of the goal work normally with the following code. The result can be seen here at jsfiddle.

jQuery(document).ready(function($) {
 $("#slider-idade").slider({
  range: "min",
  value: 18,
  min: 18,
  max: 100,
  step: 1,
  animate: 200,
  slide: function(event, ui) {
 $("#valor-idade").val(ui.value);
}
 });

 $("#valor-idade").val($("#slider-idade").slider("value"));
});

Part Two > Update Slider if the user enters a value in the text input:

------//------

I tried to follow some instructions and solutions posted in stackoverflow in English, as we can see in this stack here https://stackoverflow.com/a/7524385/1152876, however, I am not able to implement the solutions proposed in the discussion.

*Just for the record, in case there’s any interference: On the site I’m implementing Slider, I’m also using jQuery UI Touch Punch https://github.com/furf/jquery-ui-touch-punch#readme to add Touch event support to the jQuery UI library when used on mobile devices, since by default the jquery-ui library does not work.

1 answer

3


Just need to put an event to monitor when the text field had the value changed, and update the slider with this value:

$("#valor-idade").change(function(){
    $("#slider-idade").slider("value", this.value);
});

Demo no jsfiddle

  • Perfect, @bfavaretto - Works! I ended up identifying the real problem: There was a conflict with another portion of jQuery code given the way I was initiating Slider. Anyway, great solution. I gave my vote!

  • I came across a small impasse: When I enter a value in the text input, my form created with the WPCF7 (Contact Form 7) plugin is submitted given the nature of the plugin. Would you know how to prevent this from happening using jQuery? I wonder if it would be possible to identify the text input per id or class and restrict that the keypress event performed in this input submit the form?

  • It commits to typing enter, or simply putting any value in the field?

  • It submits the form only by pressing the enter key, when the focus is on the text input, by the way, with the code in question, Slider is only updated by pressing enter...but as I’m testing in the production environment, I see that you would have to use js to work around the Contact Form 7 script... The form can be seen here: http://goo.gl/bkrjRZ

  • Would it be better if I asked a new question? I am new to the community...what is the best practice at this time? would you care to inform me?

  • @Monecchi I think it’s worth a separate question for that. But I looked at your site and saw that the slider doesn’t really update when the field just loses focus, but it should, as in my example in jsfiddle. You’ve already applied my answer code?

  • Yes, it’s exactly the same code as your answer, no changes. I’m also using a code to clear the various input types on the site when in focus. I’ve updated your code on jsfiddle with the code I use to clear the fields, but I don’t see any conflict. I believe to be the javascript of the plugin itself, in this case the Contact Form 7. In this stack in English http://stackoverflow.com/a/11235672/1152876, the discussion seems promising, I will try the solutions proposed there...

  • Yes @Monecchi, the solution is exactly the one you linked. But if you have patience, it’s worth raising the question here. You can even answer yourself, this is encouraged on the site. It’s a common question, and it would be nice to have an answer here for future visitors with the same question.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.