5 Replies - 181 Views - Last Post: 20 May 2019 - 10:12 AM

#1 DarenR   User is offline

  • D.I.C Lover

Reputation: 616
  • View blog
  • Posts: 4,104
  • Joined: 12-January 10

onblur event is not returning desired results

Posted 20 May 2019 - 09:11 AM

I have onblur events that fire for certain fields to do math:
example
field 1 adds to a remainder
field 2 subtracts from the remainder
field 3 is the remainder

the problem is now they want carryover to be in the remainder field and this is where the problem lies.

since javascript always fires to execute the entire function onblur, it is returning unwanted results

example:

remainder has already a database driven amount of 10

on inWeight (field 1) you enter 5

remainder now shows 15 which would be correct

if you enter 5 on usedWeight (field 2) it should drop back to 10 however the actual results are these

start 10
in 5
15
out 5
15

because it always adds the field 1 to remainder then subtracts --

it should be like this
start 10
in 5
15
out 5
10

is there a way to not allow the bad data to flow

here is the code:

function fnWIPBLKInputCheck(txtControl, inControl) {
    var gRow = txtControl.parentNode.parentNode;
    if ((isNaN(txtControl.value)) || (txtControl.value == '') || (txtControl.value < 0)) {
        txtControl.focus();
        if (inControl == 'TAWIPBLK') {
            alert('Please enter valid BLK Added Tons');
            //gRow.cells[3].getElementsByTagName("input")[0].focus();
            gRow.cells[3].getElementsByTagName("input")[0].value = 0;
            
            return false;
        }

        if (inControl == 'CSWIPBLK') {
            alert('Please enter valid BLK Consumed tons');
            //gRow.cells[4].getElementsByTagName("input")[0].focus();
           gRow.cells[4].getElementsByTagName("input")[0].value = 0;
            
            return false;
        }
    }

   

    var TABLK = parseFloat(gRow.cells[3].getElementsByTagName("input")[0].value.replace(',', ''));
    var CSBLK = parseFloat(gRow.cells[4].getElementsByTagName("input")[0].value.replace(',', ''));
    var rmBLKExist = parseFloat(gRow.cells[5].getElementsByTagName("input")[0].value.replace(',', ''));
    var rmBLK = gRow.cells[5].getElementsByTagName("input")[0];

   
    alert(rmBLKExist);
    alert(CSBLK);
    //var remainingBLK = (TABLK - CSBLK);
//also tried ((TABLK - CSBLK)+ rmBLKExist);
    var AddedWeight = (rmBLKExist + TABLK);
    alert(AddedWeight);
    if (isNaN(AddedWeight) && isNaN(TABLK)) {
        AddedWeight = 0;
    }
    else if (isNaN(AddedWeight) && !isNaN(TABLK)) {
        AddedWeight = TABLK;

    }
    var subWeight = (AddedWeight - CSBLK);
    
    
    alert(subWeight);
    var total = subWeight;
    
    alert(total);
    rmBLK.value = total.toFixed(2);
   
    return true;
}

This post has been edited by DarenR: 20 May 2019 - 09:13 AM


Is This A Good Question/Topic? 0
  • +

Replies To: onblur event is not returning desired results

#2 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4230
  • View blog
  • Posts: 13,409
  • Joined: 08-June 10

Re: onblur event is not returning desired results

Posted 20 May 2019 - 09:24 AM

Since the names of your variables (for the input fields) don't make sense to me, can you explain which variable holds which value?

This post has been edited by Dormilich: 20 May 2019 - 09:25 AM

Was This Post Helpful? 0
  • +
  • -

#3 DarenR   User is offline

  • D.I.C Lover

Reputation: 616
  • View blog
  • Posts: 4,104
  • Joined: 12-January 10

Re: onblur event is not returning desired results

Posted 20 May 2019 - 09:37 AM

 var TABLK = parseFloat(gRow.cells[3].getElementsByTagName("input")[0].value.replace(',', '')); --- holds weight in

    var CSBLK = parseFloat(gRow.cells[4].getElementsByTagName("input")[0].value.replace(',', '')); --holds consumed weight or weight out
    var rmBLKExist = parseFloat(gRow.cells[5].getElementsByTagName("input")[0].value.replace(',', ''));  -- holds the remainder (which can come from a db and have a value greater than 0)

    var rmBLK = gRow.cells[5].getElementsByTagName("input")[0];




so it would be


rmBLKExist = rmBLKExist + TABLK -  CSBL 


which i tried and doesnt produce the correct reults
Was This Post Helpful? 0
  • +
  • -

#4 DarenR   User is offline

  • D.I.C Lover

Reputation: 616
  • View blog
  • Posts: 4,104
  • Joined: 12-January 10

Re: onblur event is not returning desired results

Posted 20 May 2019 - 09:55 AM

guess the best approach is to separate the functions out into multiple functions
Was This Post Helpful? 0
  • +
  • -

#5 Dormilich   User is offline

  • 痛覚残留
  • member icon

Reputation: 4230
  • View blog
  • Posts: 13,409
  • Joined: 08-June 10

Re: onblur event is not returning desired results

Posted 20 May 2019 - 09:58 AM

shouldn't that be rmBLK = rmBLKExist + TABLK - CSBL ?
Was This Post Helpful? 0
  • +
  • -

#6 DarenR   User is offline

  • D.I.C Lover

Reputation: 616
  • View blog
  • Posts: 4,104
  • Joined: 12-January 10

Re: onblur event is not returning desired results

Posted 20 May 2019 - 10:12 AM

i had tied this at 1st:

var remainingBLK = (rmBLKExist + TABLK - CSBLK );
 rmBLK.value = remainingBLK.toFixed(2);


however that doesnt work

i have separated my function into several functions for each field and that seams to be working..

This post has been edited by DarenR: 20 May 2019 - 10:12 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1