Carlos Martins Carlos Martins - 4 months ago 14
jQuery Question

Step-by step form showing end results

I have this Step-by step form, and at some point I have a simple calculus.

$(document).ready(function () {
// Calculate section 08
$("#txAssets,#txLiabilities").keyup(function (event) {
var assets = $("#txAssets").val();
var liabilities = $("#txLiabilities").val();
var netEquity = assets - liabilities;
$("#txNetEquity").val(netEquity);

});


How can I reuse netEquity variable in the last step of the form?
I try this:

<div id="rsEquity" class="results" onload="">Equity:<b> ?????? </b></div>


<script>
$("#rsEquity").text("Equity:" + netEquity );
</script>

Answer

You can use a window scoped variable like this:

window.netEquity = 0;
$(document).ready(function () {
                // Calculate section 08
                $("#txAssets,#txLiabilities").keyup(function (event) {
                    var assets = $("#txAssets").val();
                    var liabilities = $("#txLiabilities").val();
                    netEquity = assets - liabilities;
                    $("#txNetEquity").val(netEquity);
                });

Html:

<div class="results" onload="">Equity: <span id="rsEquity"></span></div>

Script:

<script>
$("#rsEquity").html("Equity:" + netEquity);
</script>