0

I'm using jQuery for getting the current range value. for some reason I only manage to get the default value and not the user selection.

this id the HTML:

<input id="hotel-range-min" type="range" min="0" max="200" value="20" />

and this is how I'm trying to get the current value:

 alert($("#hotel-range-min").attr('value'));

no matter if the user changes the value, the alert is always 20 and it's need to be the current value.

I've added a JSfiddle (I'm trying to get the minimum value from the range field, now it's by alert when button click):

http://jsfiddle.net/natalipolishuk/fsKh8/2/

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
user2828251
  • 235
  • 2
  • 7
  • 21

2 Answers2

1

Try

alert($("#hotel-range-min").val());

Cheers

DanielPanic
  • 184
  • 1
  • 11
  • thank you very much, it worked :) can you elaborate about the difference between what i did and your suggestion ? – user2828251 May 30 '14 at 10:35
  • *"Nevertheless, the most important concept to remember about the checked attribute is that it does not correspond to the checked property. The attribute actually corresponds to the defaultChecked property and should be used only to set the initial value of the checkbox... The same is true for other dynamic attributes, such as selected and value."* - [see here](http://api.jquery.com/attr/) – billyonecan May 30 '14 at 10:37
  • Cant really explain you the difference sorry. Both fetch the value inside a tag but ouh well it's browsers somehow the way you did dont work ;) – DanielPanic May 30 '14 at 10:41
  • 1
    @user2828251 `attr('value')` corresponds to the `defaultValue` property (ie. the value of the element when the dom is loaded, whereas `value()` gets the current value property) – billyonecan Jun 02 '14 at 13:52
0

You can also try

alert($("#hotel-range-min").prop('value'));

however if you are fetching value, its suggested you should use .val() method.

you can see the difference between val() and attr() on this link and also difference between .prop() and .attr() here

Community
  • 1
  • 1
Sid M
  • 4,354
  • 4
  • 30
  • 50