Search code examples
jqueryonkeydownonkeyup

jQuery KeyUp Not Allowing More Values


I'm new to javascript (and jQuery) so bear with me.

I want the user to be able to type in a number on a textbox and each time the user enters another value (keypress, keydown, keyup) the code executes and calculates the new value.

Here's a jsFiddle

My problem is that the textbox won't let me enter more than one value. How do I keep entering more values?

jQuery (1.9.1)

$(document).ready(function() {
    $('.conv').keyup(function() {
      var valx = parseFloat($(this).attr('data-val'));
      var classes = parseInt($('.conv').length);

      for (var i=1;classes;i++) {
          var x = 'a' + i;
          //alert(x);
          var theval = document.getElementById(x);
            var z = parseFloat($('#' + x).attr('data-val'));
            //alert(z);
            $('#' + x).val(z / valx);
        if (i == classes) {
        break;
        }
    }
    });
 });

Again...here's a jsFiddle if you want to edit


Solution

  • You seem to be overwriting even the value of the textbox that generated the event..

    Replace

    $('#' + x).val(z / valx);
    

    with

    $('#' + x).not(this).val(z / valx);
    

    EDIT

    The reason is you are always reading the value from

    var valx = parseFloat($(this).attr('data-val'));
    

    instead of

    var valx = parseFloat($(this).val());
    

    Check Fiddle