Bind Sliders to Text Inputs
Sliders visually represent a value on a scale, but in some cases, it is easier to change the value by entering it in a text input field. For example, when a user needs to input a precise number—such as setting a temperature to 22.5℃ or specifying an exact budget amount—typing the value directly may be more efficient and accurate than adjusting a slider. This demo shows how you can bind a SurveyJS Slider and Range Slider to text input fields.
Bind the Slider to a Text Input Field
To enable two-way binding between a Slider and a Single-Line Input question, follow the steps below:
- Add a Slider and a Single-Line Input question to your survey.
- Set the Slider's
nameproperty to a unique value. - Copy the Slider's
nameto the Single-Line Input question'svalueNameproperty.
This setup ensures that the Slider and the text input stay in sync—when the user changes one, the other updates automatically.
Additionally, you can configure the following optional features for better usability:
- Use the text input's
minandmaxproperties to restrict its values to the Slider's scale range. - Set the text input's
inputTypeto"number"to enforce numeric input. - Enable immediate updates by setting the text input's
textUpdateModeto"onTyping". - Place both questions in a Panel for better control over their layout and grouping.
Bind the Range Slider to Text Input Fields
Binding a Range Slider is slightly different because it returns an array of two numbers (representing the selected range), whereas a text input can only store a single value.
To synchronize a Range Slider with two Single-Line Input questions, do the following:
- Add a Range Slider and two Single-Line Input questions to your survey.
- Set the
nameproperty for all those elements to unique values. - Set the Range Slider's
setValueExpressionproperty to:[{min-input-name}, {max-input-name}]. - Specify each text input's
setValueExpressionproperty as follows:- For the minimum value input:
{range-slider-name[0]}. - For the maximum value input:
{range-slider-name[1]}.
- For the minimum value input:
This ensures the Range Slider and the input fields remain synchronized.
You can also apply the same optional settings described above—min, max, inputType, textUpdateMode, and layout grouping using a panel—for better user experience.