Calculate the remaining based on other two.

by Suraj Raj Shrestha

HTML

<table>
    <tr>
        <td>
            <input type="text" class="a" />
        </td>
        <td>
            <input type="text" class="b" />
        </td>
        <td>
            <input type="text" class="c" />
        </td>
    </tr>
</table>

CSS

input[type="text"]{
    width: 100px;
}

JavaScript

function checkIfTextIsEmpty(parent, id1, id2, id3) {
    if ($(parent).find(id2).val() != "") {
        $(parent).find(id3).val($(id1).val() * $(parent).find(id2).val());
    } else if ($(parent).find(id3).val() != "") {
        $(parent).find(id2).val($(id1).val() * $(parent).find(id3).val());
    }
}
var trParent;
$(document).on('change', '.a', function () {
    trParent = $(this).closest('tr');
    if ($(this).val() != "" || $(this).val() != undefined) {
        checkIfTextIsEmpty(trParent, this, '.b', '.c');
    }
});
$(document).on('change', '.b', function () {
    trParent = $(this).closest('tr');
    if ($(this).val() != "" || $(this).val() != undefined) {
        checkIfTextIsEmpty(trParent, this, '.a', '.c');
    }
});
$(document).on('change', '.c', function () {
    trParent = $(this).closest('tr');
    if ($(this).val() != "" || $(this).val() != undefined) {
        checkIfTextIsEmpty(trParent, this, '.b', '.a');
    }
});