Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to set default value of range input to be empty?

Tags:

I have a HTML5 range input

<input name="testing" type="range" min="0" max="10" step="1">

When there is no attribute "value" set, it sets the default value to the middle (i.e. 5 in this case). Is there a way to make it empty so that I know if the user has actively input something?

I have another div for value display. At the beginning the value will be "?", so the user will know they haven't input anything. The problem is to prevent the input from submitting value to backend if value is not actively set.

like image 357
cytsunny Avatar asked Oct 28 '16 09:10

cytsunny


People also ask

How do I set default value in input range?

By default, the minimum value of input type='range' is 0 and the maximum value is 100. We can also set the minimum and the maximum value of the input slider 🎚️ adding the min and max attributes to the input type='range' element.

What is the default value of range?

Default range is 0 to 100.

How do you style range input?

To style the range input with CSS you'll need to apply styles to two pseudo-elements: ::-webkit-slider-thumb and ::-webkit-slider-runnable-track . Find out how you can apply custom styling and make the range input more functional and appealing. Contents of the article: CSS selectors for the range input.


2 Answers

Unfortunately this is a restraint of the [type=range] element:

The default value is the minimum plus half the difference between the minimum and the maximum, unless the maximum is less than the minimum, in which case the default value is the minimum.

– HTML5 Specification

If you have no alternative other than to use this element, I'd suggest adding an extra value to your range, then checking for that value when validating the form. I.e. if your current range is 0 to 10, make your range -1 to 10, then default the value to -1; on the validation side, check if the value is not equal to -1.

like image 83
James Donnelly Avatar answered Oct 21 '22 09:10

James Donnelly


You can add an attribute "value", and set to 0,Ex:

<input type="range" min="0" max="10" value="0" />

I hope it's will help you

like image 40
Safuh Avatar answered Oct 21 '22 08:10

Safuh