0

I have form with two text inputs.

<form>
    1<input type="text" id="test" name="test" />
    2<input type="text" id="test2" name="test" />
</form>
<div id="dimensions"></div>
<div id="texte"></div>

When a user types values into these fields, jQuery automatically calculates the ratio and draws a rectangular div with width and height typed in form, but only up to 200x200 px.

My problem is that when I type, for example width=3 and height=100, my div is greater then max dimensions and I can't find where my mistake is.

$(document).ready(function () {
    $("#test, #test2").keyup(function () {
        var width = $("#test").val();
        var height = $("#test2").val();
        var max = 200;
        var min = 20;

    var ratio;
    if(width>=height){
      ratio = max / width;
      width = ratio * width;
      height = height * ratio;
    } else {
        ratio = max / height;
        height = ratio * height;
        width = width * ratio;
     };
         $("#dimensions").html(width + " x " + height);
        $("#texte").css({ "width":width + "px", "height":height + "px" });

    });
});

jsfiddle link

j08691
  • 204,283
  • 31
  • 260
  • 272
scooti
  • 51
  • 1
  • 7
  • And where is your check that says it shouldn't? – Jonathan Jul 01 '13 at 17:15
  • in the variable max = 200 – scooti Jul 01 '13 at 17:18
  • That's a declaration, not a check... – Jonathan Jul 01 '13 at 17:20
  • when I calculate ratio = max / width and ratio = max / height. After that when I calculate width or height I use this value from ratio, and result shoult not be greater than max value – scooti Jul 01 '13 at 17:24
  • `width = ratio * width;` Ah OK, I wrongfully assumed problem was with the check, I judged too soon. It's the parseInt() like Ian said. – Jonathan Jul 01 '13 at 17:27
  • `var ratio; if (width >= height) { ratio = max / width; width = ratio * width; height = height * ratio; } else { ratio = max / height; height = ratio * height; width = width * ratio; };` is really `var ratio = width >= height ? max / width : max / height; height = ratio * height; width = ratio * width;` - no need to duplicate those last two lines – Mark Schultheiss Jul 01 '13 at 17:58

4 Answers4

0

Try adding parseInt to your width and height inputs:

var width = parseInt($("#test").val());
var height = parseInt($("#test2").val());

Edit

As Adrian Trainor points out however, a ternary operator would be useful in this situation, as you don't know that the inputs are definitely numeric.

var width = $("#test").val() ? parseInt($("#test").val() : 0;
var height = $("#test2").val() ? parseInt($("#test2").val() : 0;

See here

It looks like it's doing string comparison and then checking if one is bigger than the other. See this question to understand what is going on

Community
  • 1
  • 1
Ian Clark
  • 9,237
  • 4
  • 32
  • 49
0

Make sure you handle your input correctly. The input value gives you string, not numbers. You have to convert them and make the appropriate checks, however you want.

For example (you can make more checks, depending on what you consider acceptable by the user)

http://jsfiddle.net/blackjim/ABWV6/5/

$(document).ready(function () {
    $("#test, #test2").keyup(function () {
        var width = parseInt($("#test").val(),10) || '';
        var height = parseInt($("#test2").val(),10) || '';

        if (!(typeof width === 'number' && typeof height === 'number')) {
            return false;
        }
        var max = 200;
        var min = 20;

        var ratio;
        if (width >= height) {
            ratio = max / width;
            width = ratio * width;
            height = height * ratio;
        } else {
            ratio = max / height;
            height = ratio * height;
            width = width * ratio;
        };

        $("#dimensions").html(width + " x " + height);
        $("#texte").css({
            "width": width + "px",
            "height": height + "px"
        });

    });
});
AntouanK
  • 4,880
  • 1
  • 21
  • 26
0

You need to parseint and make sure you are not doing calculations on empty strings. A ternary operator is useful for this:

var width = $("#test").val() ? parseInt($("#test").val()) : 0;
var height = $("#test2").val() ? parseInt($("#test2").val()) : 0;

This way, even if a field is empty, the calculation will be done using a 0 instead of empty string.

Adrian Trainor
  • 267
  • 1
  • 9
0

I modernized your example to correct check if width/height exists and are in 20-200 range.

See jsfiddle.net/ABWV6/9/

Alex
  • 11,115
  • 12
  • 51
  • 64