Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

The onChange does not work the second time.

Status
Not open for further replies.

haneen97

Programmer
Dec 10, 2002
280
US
Hi,
I have two functions, one is called on the onChange and the other is called on the onFocus. The onChange function is not called the second time the cursor leaves the text box. For example, if the user wants to go back and change the value in the triggering text box, the change does not happen when leaving after modifying the value. I don't think the code is necessary, but let me know if I need to paste it.

Thanks


Mo
 
This is the text box calling the two functions:

<input name="txt1Qty" type="text" id="txt1Qty" style="width:50px;" onFocus="AdjustVals(this, 'txt1')" onChange="ItemTotal(this, 'txt1')" value="<% Response.Write ("0") %>">


These are the functions:

function ItemTotal(which,txtValue)
{
var Qty=which.value;
var PrevTotal=document.getElementById('txtTotal').value * 1;
var PrevSubTotal=document.getElementById('txtSubTotal').value * 1;
var CurrReOrd=document.getElementById(txtValue+'ReOrder').value * 1;
var PrevQtyOnHand=document.getElementById(txtValue+'QtyOnHand').value * 1;
var unitprice=document.getElementById(txtValue+'UPrice').value * 1;

var NewQtyOnHand=PrevQtyOnHand - Qty;

if (Qty > PrevQtyOnHand)
{
alert('Not Enough items in stock to fulfill this order. There are/is only '+PrevQtyOnHand+' items in stock');
return (false);
}
else
if (NewQtyOnHand <= CurrReOrd)
{
alert ('ReOrder point has been reached for this product. There is enough in stock for this order');
return (false);
}
else
{
var tot = Qty * unitprice;
document.getElementById(txtValue+'QtyOnHand').value = NewQtyOnHand;
document.getElementById(txtValue+'Total').value = tot;
document.getElementById('txtSubTotal').value = PrevSubTotal + tot;
document.getElementById('txtTotal').value = PrevTotal + tot;
return (true);
}
}





function AdjustVals(which,txtValue)
{
var Qty=which.value;
if (Qty > 0)
{
var PrevTotal=document.getElementById('txtTotal').value * 1;
var PrevSubTotal=document.getElementById('txtSubTotal').value * 1;
var PrevQtyOnHand=document.getElementById(txtValue+'QtyOnHand').value * 1;
var unitprice=document.getElementById(txtValue+'UPrice').value * 1;

var NewQtyOnHand=parseInt(PrevQtyOnHand) + parseInt(Qty);
var tot = Qty * unitprice;
document.getElementById(txtValue+'QtyOnHand').value = NewQtyOnHand;
document.getElementById(txtValue+'Total').value = 0;
document.getElementById('txtSubTotal').value = PrevSubTotal - tot;
document.getElementById('txtTotal').value = PrevTotal - tot;
return (true);
}
else
{
return (false);
}
}




The idea is to allow the user to change the quantity. If the user does, I want to credit the previous values back to the stock and subtract from the totals. And, on the second onChange, I expected the top function to kick off again to update the stock and the totals with the new quantity.

Mo
 
After testing forther, I think the second onChange does take affect if the value is actually changed. But, I would like it to work no matter what the value is. I guess I am looking for an event similar to LostFocus in VB. I don't know if there is one that executes the function on leaving the box in JavaScript. I hope this helps.

Mo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top