Search code examples
javascriptjqueryjquery-uijquery-slider

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*/

Solution

  • 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"));