Unicornese
Unicornese

Reputation: 135

How can I set a default value to a textbox that displays my jquery slider value

I have a jquery slider that has a min value of 1 and a max value of 400. when I move the slider, the corresponding value is shown on a textbox. However I want to display the minimum on pageLoad both on the text box and the slider please how can I achieve thi? this is my slider code

/*slider begin*/

    $("#slider").slider({

        value:1,
        max:400,
        min:1,
        /*
         change:function(event,ui){
                     $("input#amt").val(ui.value)
                 }
         */

        slide: function(event, ui) {   $("#amt").val(ui.value) }

    });

     $("#amt").change(function(event) {
   var data = $("#amt").val();
   if (data.length > 0) 
   {
      if (parseInt(data) >= 0 && parseInt(data) <= 400) 
      {
          $("#slider").slider("option", "value", data);
      }
      else
      {
        if (parseInt(data) < 0) 
        {
          $("#amt").val("0");
          $("#slider").slider("option", "value", "0");
        }
        if (parseInt(data) > 400) 
        {
          $("#amt").val("400");
          $("#slider").slider("option", "value", "400");
        }
     }
   }
    else
    { 
      $("#slider").slider("option", "value", "1"); 
    }   
   });

    /*slider end*/

Upvotes: 2

Views: 722

Answers (2)

woggles
woggles

Reputation: 7444

Your slider should be set to 1 as is - the value option of the slider does the following. From the API:

Determines the value of the slider, if there's only one handle. If there is more than one handle, determines the value of the first handle.

Code examples

Initialize a slider with the value option specified.

$( ".selector" ).slider({ value: 37 });

You can set the value of the textbox as follows:

$("#amt").val(1);

Upvotes: 0

Asciiom
Asciiom

Reputation: 9975

You can set the slider value first:

$("#slider").slider( "value" , 1);

And then read it to set the text box value (or just set the text box value to -1 as well but that's less pretty)

$("#amt").val($("#slider").slider( "value"));

Upvotes: 2

Related Questions