How to do the same in JavaScript? [closed]

If you want Vanilla JavaScript solution you can try this:

function startCalc() {
    var root = document.documentElement || document.body;
    root.addEventListener('blur', function(e) {
        var node = e.target;
        if (node.nodeName == 'input' &&
            node.getAttribute('type') == 'text') {
            var imekontrolebase = node.getAttribute('id');
            var ime = imekontrolebase.substr(12, imekontrolebase.length);
            var n = ime.indexOf("_");
            var red = ime.substr(n+1);

            var imekol1 = "txt_OBR_P_1_1_" + red;                                    
            var imekol2 = "txt_OBR_P_1_2_" + red;
            var imekol3 = "txtKOL3_" + red;

            var txt_OBR_P_1_1 = parseFloat(document.getElementById(imekol1).value.replace(/\,/g, '')) || 0
            var txt_OBR_P_1_2 = parseFloat(document.getElementById(imekol2).value.replace(/\,/g, '')) || 0

            var txtKOL3 = txt_OBR_P_1_1 + txt_OBR_P_1_2;

            if (txtKOL3 === 0 || txtKOL3 === Infinity || isNaN(txtKOL3)) {                                       
                txtKOL3 = document.getElementById(imekol3).value;
            }
            else {
                var ukupnoID3 = ("" + txtKOL3).replace(/(\d)(?=(\d\d\d)+(?!\d))/g, function ($1) { return $1 + "," });
                document.getElementById(imekol3).value = ukupnoID3;
            }
        })

    });
}

basically

  • $('#name') -> document.getElementById('name'); or
  • document.querySelector('#name') that return single element or
  • document.querySelectorAll('#name') that return array like object

  • jqueryObject.val() -> htmlNode.value

  • $(document).on('blur' -> node.addEventListener('blur' and you need to check for the correct node using event.target

Leave a Comment