Search code examples
jqueryclonekeyup

set input value clone


Here's what I have working: 3 textfields that are cloned with a math function A / B = C. Works perfectly.

Problem: I'd like to set the textfield value "on keyup" of A, B, and C so that the value actually reads what's typed into the textfield. (see "NOT RIGHT" in code comments)

http://jsfiddle.net/CzZxf/

$("#math-table input").live("keyup", function(){
   var id = this.id.match(/\d+/);
   $("#C"+id).val( Math.round (($("#A"+id).val() / $("#B"+id).val()) * 100) + "%"  );     
   $('#A1'+id).attr('value', ($('#A1'+id).val()));  // NOT RIGHT?
   $('#B1'+id).attr('value', ($('#B1'+id).val()));  // NOT RIGHT?
   $('#C1'+id).attr('value', ($('#C1'+id).val()));  // NOT RIGHT?
});

var uniqueIds = $("#math-table tr").length;
$("#math-table input[id^='B']").live("change", function(){
    var $thisRow = $(this).closest("tr"),
    $clone = $thisRow.clone(),             // Clone row
    $inputs = $clone.find("input").val("");// Reset values, return all inputs
    uniqueIds++; //Increment ID
    $inputs[0].id = "A" + uniqueIds;
    $inputs[1].id = "B" + uniqueIds;
    $inputs[2].id = "C" + uniqueIds;
    $thisRow.after($clone);                    
});

Solution

  • So you want the literal value attribute to be populated? You just about had it: http://jsfiddle.net/CzZxf/1/ (tested in CHROME)