/    Sign up×
Community /Pin to ProfileBookmark

use onBlur instead of button

Hi all,
I have a script I am working on (thanks to people on this forum). I would like to remove the “calculate” buttons and use onBlur for each price textbox to run the function… here is what I have so far:

<script type=”text/javascript”>

function setEvent(){

var but, i=1;
while(but=document.getElementById(‘calc_’+(i++))){
but.onclick=calculate
}
}

function calculate(){
var f=this.form, grandtotV=0, j=1, i=1, but;
while(but=document.getElementById(‘calc_‘+(j++))){
var oQty=f[‘qty_
‘+i];
var oPrice=f[‘price_’+i];
i++;
var subV=Number(oQty.value)*Number(oPrice.value);
var totV=subV;
grandtotV+=totV;
}
f[‘partstotal’].value=grandtotV.toFixed(2);
f[‘tax’].value = (f[‘partstotal’].value *
f[‘taxrate’].value).toFixed(2);
f[‘gtotal’].value = ((f[‘partstotal’].value * f[‘taxrate’].value) + grandtotV).toFixed(2);
}

onload=setEvent
</script>

<form action=””>

Quantity: <input type=”text” name=’qty_1′><br>
Price: <input type=”text” name=’price_1′ onBlur=”javascript:calculate()”><br>
<input type=”button” value=”Calculate” id=”calc_1″>
<br><br>
Quantity: <input type=”text” name=’qty_2′><br>
Price: <input type=”text” name=’price_2′ onBlur=”javascript:calculate()”><br>
<input type=”button” value=”Calculate” id=”calc_2″>
<br><br>
Quantity: <input type=”text” name=’qty_3′><br>
Price: <input type=”text” name=’price_3′ onBlur=”javascript:calculate()”><br>
<input type=”button” value=”Calculate” id=”calc_3″>
<br><br>
Tax Rate: <input type=”text” name=”taxrate” value=”0.0825″>
<br>Tax: <input type=”text” name=”tax” readonly=”readonly”>
<br>Parts Total: <input type=”text” name=”partstotal” readonly=”readonly”>
<br>Grand Total:<input type=”text” name=”gtotal” readonly=”readonly”>
</form>

to post a comment
JavaScript

1 Comments(s)

Copy linkTweet thisAlerts:
@phrank80May 15.2009 — Since you only need to run the calculate function now:

You can just do it like this. The only downside is that I am setting the loop in the function to a static number.

In this code below I changed the format to clean it up so I could see it on my test page.

[CODE]<script type="text/javascript">

function calculate(){
var f=document.forms[0], grandtotV=0, j=1, i=1, but;
//while(but=document.getElementById('calc_'+(j++))){
for(x=0; x<3; x++){ // where 3 is the number of field sets
var oQty=f['qty_'+i];
var oPrice=f['price_'+i];
i++;
var subV=Number(oQty.value)*Number(oPrice.value);
var totV=subV;
grandtotV+=totV;
}
//}
f['partstotal'].value=grandtotV.toFixed(2);
f['tax'].value = (f['partstotal'].value * f['taxrate'].value).toFixed(2);
f['gtotal'].value = ((f['partstotal'].value * f['taxrate'].value) + grandtotV).toFixed(2);
}

onload=setEvent
</script>

<form method="post">
<table>
<tr>
<td>Quantity: </td>
<td><input type="text" name='qty_1'></td>
<td>Price: </td>
<td><input type="text" name='price_1' onBlur="calculate();"></td>
</tr>
<tr>
<td>Quantity: </td>
<td><input type="text" name='qty_2'></td>
<td>Price:</td>
<td><input type="text" name='price_2' onBlur="calculate();"></td>
</tr>
<tr>
<td>Quantity: </td>
<td><input type="text" name='qty_3'></td>
<td>Price: </td>
<td><input type="text" name='price_3' onBlur="calculate()"></td>
</tr>
<tr>
<td colspan="4">&nbsp;</td>
</tr>
<tr>
<td colspan="4">Tax Rate: <input type="text" name="taxrate" value="0.0825"></td>
</tr>
<tr>
<td colspan="4">Tax: <input type="text" name="tax" readonly="readonly"></td>
</tr>
<tr>
<td colspan="4">Parts Total: <input type="text" name="partstotal" readonly="readonly"></td>
</tr>
<tr>
<td colspan="4">Grand Total:<input type="text" name="gtotal" readonly="readonly"></td>
</tr>
</table>
</form>[/CODE]


-Frank
×

Success!

Help @newbe101 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...