I created a calculator for CTR with jQuery. The calculator is working just the way I want, but I'm quite new to jQuery and I wonder if I am using jQuery effectively.
The goal of the code is that if 2 values are entered and the button is clicked then the correct function is called and the output's background colour will change.
<div class="container">
<div class="alert alert-info" role="alert">
<span class="align-middle">CTR Calculator</span>
</div>
<div>
<label for="v1">Number of Impressions:</label> <br/>
<input id="v1" type="text" style="width:10em;"/> <br/>
<label for="v2">Number of Clicks:</label> <br/>
<input id="v2" type="text" style="width:10em;"/> <br/>
<label for="v3">CTR:</label><br/>
<input id="v3" type="text" style="width:10em;"/> <br/>
<div>
<button id ="btn" type="button" class="btn btn-secondary">Calculate</button>
</div>
<div class="alert alert-info result" role="alert">
<span id="result" class="align-middle"></span>
</div>
$(document).ready(function(){
$("#btn").on("click",function(event){
if ($("#v1").val() === '' && $("#v2").val() === '' && $("#v3").val() === '') {
$('#v1,#v2,#v3').css('background-color', 'transparent');
alert("Please enter atleast 2 values!");
} else if ($("#v1,#v2").val() === '' || $("#v1,#v3").val() === '' || $("#v2,#v3").val() === '') {
$('#v1,#v2,#v3').css('background-color', 'transparent');
alert("Please enter atleast 2 values!");
} else if ($("#v1").val() === '') {
var mathImp = (parseInt($('#v2').val()) / parseInt($('#v3').val())) * 100;
$('#v1').val(mathImp);
$('#v1').css('background-color', 'beige');
$('#v2,#v3').css('background-color', 'transparent');
} else if ($("#v2").val() === '') {
var mathClicks = (parseInt($('#v1').val()) * parseInt($('#v3').val())) / 100;
$('#v2').val(mathClicks);
$('#v2').css('background-color', 'beige');
$('#v1,#v3').css('background-color', 'transparent');
} else if ($("#v3").val() === '') {
var mathCTR = (parseInt($('#v2').val()) / parseInt($('#v1').val())) * 100;
$('#v3').val(mathCTR);
$('#v3').css('background-color', 'beige');
$('#v1,#v2').css('background-color', 'transparent');
} else {
alert("Please enter only 2 values!");
}
});
});
"Please enter atleast 2 values!"
) \$\endgroup\$