L84
L84

Reputation: 46308

Read a Value from an Input after Change

I have a range slider that will allow the user to easily change the input. I need to read the value after they change the slider.

I originally tried:

var buzzReview = $( "#Cat_Custom_1" ).val();

But that just gives me the value before the slider is moved (which is blank).

I then considered doing something like this:

$("#Cat_Custom_1").on("change",function(){
  var buzzReview = $( "#Cat_Custom_1" ).val();
});

The problem with this is the variable isn't available outside of that function.

How do I obtain the value of the slider and store it in a variable that is accessible to my entire script regardless of when it is changed? IE: User changes slider and I have the value, then the user changes the slider later and again I have the new value.

I am using jQuery 2.1. Here is a fiddle with working code.

Upvotes: 0

Views: 58

Answers (2)

Jekk
Jekk

Reputation: 598

Perhaps you should try :

 $(function() {


   //your slider setUp was here



     function getSliderValue(){
        return $("#CAT_Custom_1").val();
     }

     $("button").click(function(){
       alert(getSliderValue());
     });

  });

I just simply made a function that returns the current value of that input type. Now you just tweak it a little, to avoid the empty value instead of 0. :) From somewhere outside, call the function and store the returned value where you need it.

Upvotes: 0

Se0ng11
Se0ng11

Reputation: 2333

I had fork your fiddle, and it work great, the problem is that jquery is case sensitive, thats why you get undefined value

http://jsfiddle.net/zXV4s/8/

if you want to make it into variable, this is the fiddle

http://jsfiddle.net/zXV4s/11/

$(".button").on("click",function(ev){
    alert($( "#CAT_Custom_1").val());  
});

Upvotes: 2

Related Questions