Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML range: set value attribute dynamically

Good evening!!

I'm using HTML input type range to create a slidebar. I would like to dynamically change the attribute "value" (default position of the cursor) before displaying the slidebar.

I can retrieve the value I need (from localStorage) but I don't manage to set it!!

Here is the range object:

<input type="range" min="0" max="1050" value="0" step="30"  onchange="showValue(this.value); changeScrollBar(this.value);"/>

Now it is set to 0 but I would like to use a variable to change it (i.e I'll set a new variable foo=localStorage.getItem("foo2") then I would like to use value=foo in range)!

Any clue?

Thanks a lot!!!

like image 588
Silver18 Avatar asked Sep 11 '12 20:09

Silver18


People also ask

How do you set a range of numbers in HTML?

The <input type="range"> defines a control for entering a number whose exact value is not important (like a slider control). Default range is 0 to 100. However, you can set restrictions on what numbers are accepted with the attributes below. Tip: Always add the <label> tag for best accessibility practices!

What type of HTML tag is used to show the current set value of a slider?

The "range" tag (actually a slider) in HTML 5 adds a ver useful HTML form control. In the IE browser, the slider value is displayed when you move the slider.


1 Answers

You get the DOM element for the <input> tag and then use:

elem.value = foo;

If you add an ID to the input tag like this:

<input id="mySlider" type="range" min="0" max="1050" value="0" step="30"  onchange="showValue(this.value); changeScrollBar(this.value);"/>

Then, you can do the whole thing like this:

var input = document.getElementById("mySlider");
input.value = localStorage.getItem("foo2");
like image 164
jfriend00 Avatar answered Oct 13 '22 04:10

jfriend00