Search code examples
javascriptjqueryjquery-slider

Problems with jQuery slider and input


I'm trying to make my first jQuery slider, and I have this code:

$("#str").keyup(function(event) {
var data=$("#str").val();
if (data.length>0) 
{
 var intdata=SpecialRound(parseFloat(data));
 if (intdata>=0 && intdata<=20) 
 {
  $("#str_slider").slider("option", "value", data);
 }
 else
 {
  if (intdata<0) 
  {
   $("#str").val("0");
   $("#str_slider").slider("option", "value", "0");
  }
  if (intdata>20) 
  {
   $("#str").val("20");
   $("#str_slider").slider("option", "value", "20");
  }
 }
}
});

Although, I have a problem. When I write something in the input with the ID #str, nothing happens. Though, the sliders works perfect. You can test att http://vpopulus.tk/damage

EDIT: I basicly want to make so when you edit the input, the slider is changed too.


Solution

  • I think your selector $('#str') is the problem, because your input has no ID #str but just a name attribute with value "str". The correct selector would be $('input[name="str"]')

    EDIT: The actual problem is this SpecialRound function because you're using a not-existing function "round()" there. Use this instead:

    //Special round
    function SpecialRound(f) {
       return Math.round(f*100) / 100;
    }