I have the following code that provides me with the calculation that I need, but I want it to work with multiple outputs.
For example, Part A and Part B would have the same jQuery, except Part B may have a different multiplier or calc3
, etc.
I will have up to 50 parts and am trying to prevent from having to write 50 rows of jQuery. Is there a way to write this with the (this) variable instead of the ('#id'
) ?
In the Snippet below, if you add in the Qty, Width, Height, and Depth - you will see the calculation work for Part A, but not Part B because I am trying to make the jQuery calculations simple.
//part1
$("#part1d").keyup(function() {
//part A
$('#part1A').val($('#part1q').val() * 1);
$('#part1AL').val($('#part1w').val() - $('#calc1').val());
$('#part1AW').val($('#part1d').val() - $('#calc2').val());
//part B
$('#part1A').val($('#part1q').val() * 2);
$('#part1AL').val($('#part1w').val() - $('#calc1').val());
$('#part1AW').val($('#part1d').val() - $('#calc2').val());
});
* {
margin: 0;
padding: 0;
float: left;
}
.wrap {
width: 96%;
margin: 2% 2% 500px 2%;
}
.partwrap {}
.partname {
width: 20%;
margin: 1% 20% 1% 0;
}
.partdata {
width: 10%;
}
input {
width: auto;
float: none;
}
.sectiontitle {
color: red
}
.partdescr {
color: purple;
}
.parts {
width: 150px;
}
.values {
display: none;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!--values-->
<input class="values" id="calc1" value="1.4375">
<input class="values" id="calc2" value=".25">
<div class="wrap">
<!--part 1-->
<!--part 1 input-->
<div class="partwrap">
<div class="partdescr">
<div class="partname">Part Number: <input id="part1"></div>
<div class="partdata">Qty <input id="part1q"></div>
<div class="partdata">Width <input id="part1w"></div>
<div class="partdata">Height <input id="part1h"></div>
<div class="partdata">Depth <input id="part1d"></div>
</div>
<!--part 1 output-->
<div class="partdescr">
<div class="parts">Part Name</div>
<div class="parts">Qty</div>
<div class="parts">Length</div>
<div class="parts">Width</div>
<div class="parts">Height</div>
</div>
<div>
<div class="parts">Part A</div>
<input id="part1A" class="parts">
<input id="part1AL" class="parts">
<input id="part1AW" class="parts">
<input id="part1AH" class="parts">
</div>
<div>
<div class="parts">Part B</div>
<input id="part1A" class="parts">
<input id="part1AL" class="parts">
<input id="part1AW" class="parts">
<input id="part1AH" class="parts">
</div>
</div>
</div>