/    Sign up×
Community /Pin to ProfileBookmark

How Do You Remove The Zeros?

I’ve been working with an excellent script that executes a sum based upon a ID declaration in <td>

Unfortunately the script adds zeros into the total fields. Could any one here nudge me in the right direction as to how to tell the script to not print show zeros in rows without a calculation? It fine if they appear on lines that contains numbers for the sum.

Thanks for the help. Below shows the script in action.

[code=html]
<html>
<head>
<script type=”text/javascript”>

function totFields(){

puts=document.getElementById(“r1”).getElementsByTagName(“input”).length

rowCount=0

while(document.getElementById(“r”+rowCount)){
subTotal=0
currentRow=document.getElementById(“r”+rowCount)
rowInputs=currentRow.getElementsByTagName(“input”)

for(var i=0;i<rowInputs.length;i++){

if(rowInputs[i].type==”text”&&rowInputs[i].name.indexOf(“subtotal”)==-1){
subTotal+=rowInputs[i].value*1
}

}

document.getElementById(“subtotal”+rowCount).value=subTotal
rowCount++
}

/* Sutotal gets a zro OnChange here I belive this is where the problem resides. */
total=0
for(var j=0;j<rowCount;j++){
total+=document.getElementById(“subtotal”+j).value*1
}

document.getElementById(“gtotal”).value=total
}

</script>
<meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1″ /><style type=”text/css”>
<!–
body {
margin-left: 50px;
margin-right: 50px;
margin-top: 50px;
margin-bottom: 50px;
}
–>
</style></head>

<body>

<form method=”POST” action=”” id=”totForm”>
<p>
</p>
<h3>OnChange form calculation. Total prints &quot;0&quot; Problem</h3>
<p></p>
<table width=”900″ colspan=”1″>
<tr bgcolor=”#F3F3F3″>
<td class=”center”>MON</td>
<td class=”center”>TUE</td>
<td class=”center”>WED</td>
<td class=”center”>THR</td>
<td class=”center”>FRI</td>
<td class=”center”>SAT</td>
<td class=”center”>SUN</td>
<td class=”center”>Total</td>
</tr>
<tr bgcolor=”#F3F3F3″ id=”r0″>
<td class=”center”><input type=”text” size=”3″ name=”MON1″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”TUE1″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”WED1″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”THU1″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”FRI1″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”SAT1″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”SUN1″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”form[subtotal0]” id=”subtotal0″ /></td>
</tr>
<tr bgcolor=”#F3F3F3″ id=”r1″>
<td class=”center”><input type=”text” size=”3″ name=”MON2″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”TUE2″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”WED2″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”THU2″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”FRI2″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”SAT2″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”SUN2″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”form[subtotal1]” id=”subtotal1″ /></td>
</tr>
<tr bgcolor=”#F3F3F3″ id=”r2″>
<td class=”center”><input type=”text” size=”3″ name=”MON3″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”TUE3″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”WED3″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”THU3″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”FRI3″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”SAT3″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”SUN3″ onChange=”totFields();” /></td>
<td class=”center”><input type=”text” size=”3″ name=”form[subtotal2]” id=”subtotal2″ /></td>
</tr>
<tr bgcolor=”#F3F3F3″>
<td colspan=”4″>&nbsp;</td>
<td class=”center”>&nbsp;</td>
<td class=”center”>&nbsp;</td>
<td class=”center”>Grand Total:</td>
<td class=”center”><input type=”text” size=”3″ name=”gtotal” id=”gtotal” /></td>
</tr>

</table>

</form>
<p>If you enter a number in the upper right field you will see zeros appear in all total fields. This is not wanted. How can I have them not appear on rows with no calculation?</p>

</body>
</html>
[/code]

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@jwstockwellFeb 08.2012 — Try this - see my comments //

function totFields() {

// keep track of the blanks with this
var haveNAN

puts = document.getElementById("r1").getElementsByTagName("input").length

rowCount = 0

while (document.getElementById("r" + rowCount)) {
subTotal = 0
currentRow = document.getElementById("r" + rowCount)
rowInputs = currentRow.getElementsByTagName("input")

// init for each row
haveNAN = 0

for (var i = 0; i < rowInputs.length; i++) {

if (rowInputs[i].type == "text" && rowInputs[i].name.indexOf("subtotal") == -1) {
subTotal += rowInputs[i].value * 1
}

// add 1 if blank
if (trim(rowInputs[i].value) == "") {
haveNAN += 1
}
}

document.getElementById("subtotal" + rowCount).value = subTotal

// if all are blank, make subtotal ""
if (haveNAN == rowInputs.length) {
document.getElementById("subtotal" + rowCount).value = ""
}

rowCount++
}

/* Sutotal gets a zro OnChange here I belive this is where the problem resides. */
total = 0
for (var j = 0; j < rowCount; j++) {
total += document.getElementById("subtotal" + j).value * 1
}

document.getElementById("gtotal").value = total
}
Copy linkTweet thisAlerts:
@nap0leonFeb 08.2012 — Added a "validSubTotal" flag inside the loop. It gets set to true when a non-empty value is added to subtotal.

<i>
</i>&lt;script type="text/javascript"&gt;

function totFields(){
puts=document.getElementById("r1").getElementsByTagName("input").length
rowCount=0

<i> </i>while(document.getElementById("r"+rowCount)){
<i> </i> subTotal=0
<i> </i> validSubTotal = false
<i> </i> currentRow=document.getElementById("r"+rowCount)
<i> </i> rowInputs=currentRow.getElementsByTagName("input")

<i> </i> for(var i=0;i&lt;rowInputs.length;i++){
<i> </i> if(rowInputs[i].type=="text"&amp;&amp;rowInputs[i].name.indexOf("subtotal")==-1){
<i> </i> if (!(rowInputs[i].value == '')) { validSubTotal = true }
<i> </i> subTotal+=rowInputs[i].value*1
<i> </i> }
<i> </i> }

<i> </i> if (validSubTotal) {
<i> </i> document.getElementById("subtotal"+rowCount).value=subTotal
<i> </i> }
<i> </i> rowCount++
<i> </i>}

/* Sutotal gets a zro OnChange here I belive this is where the problem resides. */
total=0
for(var j=0;j&lt;rowCount;j++){
total+=document.getElementById("subtotal"+j).value*1
}

<i> </i>document.getElementById("gtotal").value=total
}

&lt;/script&gt;
Copy linkTweet thisAlerts:
@surge42authorFeb 08.2012 — Thanks fellas...

I appreciate the help.
Copy linkTweet thisAlerts:
@007JulienFeb 08.2012 — A variant consist to add only this end of line !
[CODE]document.getElementById("subtotal"+rowCount).value=subTotal[COLOR="Red"]?subtotal:'';[/COLOR][/CODE]
×

Success!

Help @surge42 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.17,
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,
)...