I'm trying to build a function that will trigger on focusin / focusout and show the result in a <span>
when I entering data in the inputs fields.
My doc-ready function is in the <head>
and the code below is call in a script tag after the <body>
.
Here's a screenshot: This is the input fields and the span that shows the result
Code:
var $costPrice = parseInt($("#cost-price").val()); // Cost price <div>
var $markupPercentage = parseInt($("#markup-percentage").val()); // Markup <div>
var $sellingPrice = ($markupPercentage / 100) * $costPrice + $costPrice;
var $finalPrice = $("#final-price"); // This <span> tag that receive the result of $sellingPrice
function showPrice($sellingPrice, $finalPrice){
if ($sellingPrice !== null) {
$finalPrice.html("$" + $sellingPrice);
} else {
$finalPrice.html("$0"); // Here I want to replace Nan by $0
}
};
$costPrice.focusout(showPrice); // I want to trigger/show the result.
$markupPercentage.focusout(showPrice); // I want to trigger/show the result.
If I enter a value in the inputs and run this code below in the console, it works. But that's not interactive. I'd like to get the same result but on focusin / focusout input fields.
var $costPrice = parseInt($("#cost-price").val()); // Cost price <div>
var $markupPercentage = parseInt($("#markup-percentage").val()); // Markup <div>
var $sellingPrice = ($markupPercentage / 100) * $costPrice + $costPrice;
var $finalPrice = $("#final-price").html("$" + $sellingPrice);
Thanks for help !
Wouldn't
active
andblur
work? Active would be when someone ckicks on the inputs, and blur when someone "unselections" it