I developed a percentage change calculator with jquery. It is working file. But problem is, I can’t make multiple calculator with same jquery codes. Now each calculator need special codes and class for it. Is it possible muliple calculator without adding any extra codes ? Codepen preview : https://codepen.io/toolsim/pen/ZEoYbgY . Please help me…
$(document).on("change keyup blur live", ".ChangeCalulator", function() { // For:- = Topper To Current %
///////////////////
var first = Number($('.From101').val()); // = Topper Price
var second = Number($('.To101').val()); // = Current Price
var minus = second - first; // 2000 - 1000 = {1000 = minus}
var divide = (minus / first); // 1000 / 1000 = 1
var multiply = divide * 100; // 1 * 100 = 100%
$('.Result101').val(Number(multiply).toFixed(2)); // = Topper to Current %
///////////////////////
var first = Number($('.From102').val()); // = Topper Price
var second = Number($('.To102').val()); // = Current Price
var minus = second - first; // 2000 - 1000 = {1000 = minus}
var divide = (minus / first); // 1000 / 1000 = 1
var multiply = divide * 100; // 1 * 100 = 100%
$('.Result102').val(Number(multiply).toFixed(2)); // = Topper to Current %
});
/////////////////////////
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<input type="text" class="ChangeCalulator From101" value="">
<input type="text" class="To101" value="25000">
<input type="text" class="Result101" value="">
<br><br>
<input type="text" class="ChangeCalulator From102" value="">
<input type="text" class="To102" value="25000">
<input type="text" class="Result102" value="">
<br><br>
2
Answers
Yes, treat each one individually, by wrapping it inside a container
.section
. Use an event handler on each of the required inputs, useinput
event, then by finding parent element you can find other elements in same "line".You can try following code:
For HTML:
For Jquery: