/    Sign up×
Community /Pin to ProfileBookmark

Default values in text input fields

I am using a javascript to add numerical input values together to give a user answers. What I want to be able to do is have the have a default display of 0 for every input field. When the field is clicked on I want the 0 to disappear so that it doesn’t have to be highlighted by the user. If that cannot be done easily then I will need to have to be able to have the javascript that will input a 0 for every field that was blank and give the totals to the user.

Java:

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

function domath(myform) {
var a = parseFloat( myform.field1.value );
var b = parseFloat( myform.field2.value );
var c = parseFloat( myform.field3.value );
var d = parseFloat( myform.field4.value );
var e = parseFloat( myform.field5.value );
var f = parseFloat( myform.field6.value );
var g = parseFloat( myform.field7.value );
var h = parseFloat( myform.field8.value );
var i = parseFloat( myform.field9.value );
var j = parseFloat( myform.field10.value );
var k = parseFloat( myform.field11.value );
var l = parseFloat( myform.field12.value );
var m = parseFloat( myform.field13.value );
var n = parseFloat( myform.field14.value );
var o = parseFloat( myform.field15.value );
var p = parseFloat( myform.field16.value );
var q = parseFloat( myform.field17.value );
var r = parseFloat( myform.field18.value );
var s = parseFloat( myform.field19.value );
var t = parseFloat( myform.field20.value );
var u = parseFloat( myform.field21.value );
var v = parseFloat( myform.field22.value );
var w = parseFloat( myform.field23.value );
var x = parseFloat( myform.field24.value );

if (isNaN(a) || isNaN(b) || isNaN(c) || isNaN(d) || isNaN(e) || isNaN(f) || isNaN(g) || isNaN(h) || isNaN(i) || isNaN(j) || isNaN(k) || isNaN(l) || isNaN(m) || isNaN(n) || isNaN(o) || isNaN(p) || isNaN(q) || isNaN(r) || isNaN(s) || isNaN(t) || isNaN(u) || isNaN(v) || isNaN(w) || isNaN(x)) {
alert( ‘entries must be a numbers – try again’ );
return;
}
myform.txtTotal1.value = parseFloat( a + b + c + d + e + f + g + h + i + j + k + l ).toFixed(3);
myform.txtTotal2.value = parseFloat( m + n + o + p + q + r + s + t + u + v + w + x ).toFixed(3);

}
function processT1()
{
var Total1, Total2;

var Total1 =
parseFloat(document.myform.Total1.value);
var Total2 =
parseFloat(document.myform.Total2.value);
var y =
parseFloat(document.myform.field25.value);

var T1 = Total1 + Total2 + y;

document.myform.T1.value = T1.toFixed(3);
}

function processT2()
{
var T1;

var T1 =
parseFloat(document.myform.T1.value);

var z =
parseFloat(document.myform.field26.value);

var T2 = T1 + z;

document.myform.txtT2.value = T2.toFixed(3);
}

</script>
[/code]

HTML:

[code]
<table style=”width: 800px;text-align:center;”
cellpadding=”0″ cellspacing=”10″ align=”center”>
<tr> <td height=”369″ width=”900″ align=”center” valign=”top”>&nbsp;</td></tr>
<form name=”myform” id=”myform” action=”#” method=”GET”>
<tr>
<td valign=”top” align=”center”><table style=”width: 800px;”
cellpadding=”0″ cellspacing=”0″ align=”center”>

<tr valign=”top”><td>Label</td>
<td><input id=”field25″ type=”text” class=”fields” size=”15″></td>
<td>
Label</td>
<td valign=”top”><input id=”field26″ type=”text” class=”fields” size=”15″></td></tr></table> </td></tr>

<tr>
<td colspan=”4″ align=”center”><table width=”800″ align=”center” cellpadding=”0″ cellspacing=”0″>
<tr><td>&nbsp;</td>
<td align=”left” valign=”top”>First Set of Numbers</td>
<td align=”left” valign=”top”>Second Set of Numbers</td></tr>
<tr><td>Label</td>
<td><input id=”field1″ type=”text” class=”fields”></td>
<td><input id=”field13″ type=”text” class=”fields”></td></tr>
<tr><td colspan=”3″ height=”5″></td></tr>
<tr><td>Label</td>
<td><input id=”field2″ type=”text” class=”fields”></td>
<td><input id=”field14″ type=”text” class=”fields”></td></tr>
<tr><td colspan=”3″ height=”5″></td></tr>
<tr><td>Label</td>
<td><input id=”field3″ type=”text” class=”fields”></td>
<td><input id=”field15″ type=”text” class=”fields”></td></tr>
<tr><td colspan=”3″ height=”5″></td></tr>
<tr><td>Label</td>
<td><input id=”field4″ type=”text” class=”fields”></td>
<td><input id=”field16″ type=”text” class=”fields”></td></tr>
<tr><td colspan=”3″ height=”5″></td></tr>
<tr><td>Label</td>
<td><input id=”field5″ type=”text” class=”fields”></td>
<td><input id=”field17″ type=”text” class=”fields”></td></tr>
<tr><td colspan=”3″ height=”5″></td></tr>
<tr><td>Label</td>
<td><input id=”field6″ type=”text” class=”fields”></td>
<td><input id=”field18″ type=”text” class=”fields”></td></tr>
<tr><td colspan=”3″ height=”5″></td></tr>
<tr><td>Label</td>
<td><input id=”field7″ type=”text” class=”fields”></td>
<td><input id=”field19″ type=”text” class=”fields”></td></tr>
<tr><td colspan=”3″ height=”5″></td></tr>
<tr><td>Label</td>
<td><input id=”field8″ type=”text” class=”fields”></td>
<td><input id=”field20″ type=”text” class=”fields”></td></tr>
<tr><td colspan=”3″ height=”5″></td></tr>
<tr><td>Label</td>
<td><input id=”field9″ type=”text” class=”fields”></td>
<td><input id=”field21″ type=”text” class=”fields”></td></tr>
<tr><td colspan=”3″ height=”5″></td></tr>
<tr><td>Label</td>
<td><input id=”field10″ type=”text” class=”fields”></td>
<td><input id=”field22″ type=”text” class=”fields”></td></tr>
<tr><td colspan=”3″ height=”5″></td></tr>
<tr><td>Label</td>
<td><input id=”field11″ type=”text” class=”fields”></td>
<td><input id=”field23″ type=”text” class=”fields”></td></tr>
<tr><td colspan=”3″ height=”5″></td></tr>
<tr><td>Label</td>
<td><input id=”field12″ type=”text” class=”fields”></td>
<td><input id=”field24″ type=”text” class=”fields”></td></tr>
<tr><td colspan=”3″ height=”5″></td></tr>
<tr>
<td colspan=”3″ align=”center”
valign=”top” width=”800″>
<table width=”400″
cellpadding=”0″ cellspacing=”5″ align=”center”>
<tbody>

<tr>
<td style=”text-align: center;”><input id=”Button1″
value=”Calculate” type=”button” value=”Get results”
onclick=”domath(form)” class=”button”>
</td>
<td style=”text-align: center;”><input
id=”Reset1″ value=”Reset” type=”reset” class=”button”> </td>

</tr></table></td></tr><tr><td height=”5″>&nbsp;</td></tr>

<tr><td colspan=”3″ align=”center”><table align=”center” cellpadding=”0″ cellspacing=”0″ width=”100%”><tr><td>Total</td>
<td><input id=”Total1″ type=”text” class=”fields”></td>
<td><input id=”Total2″ type=”text” class=”fields”></td></tr>
<tr><td colspan=”3″ align=”center”> </td></tr>
<tr><td colspan=”3″ height=”5″>&nbsp;</td></tr>
<tr><td>Total 1</td><td align=”center”><input type=”button” value=”Process”
name=”btnCalculate” onClick=”processT1()” class=”button”></td>
<td align=”center”><input id=”T1″ type=”text” class=”fields”></td></tr><tr><td colspan=”3″ height=”5″>&nbsp;</td></tr>
<tr><td>Total 2</td>
<td align=”center”><input type=”button” value=”Process”
name=”btnCalculate” onClick=”processT2()” class=”button”>
<td align=”center”><input id=”T2″ type=”text” class=”fields”></form></td></tr></table></td></tr></table></td></tr>
</table>
[/code]

Thanks in advance

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERMar 01.2009 — For each field that it applies, add a value = "0" to the <input> statement.
<i>
</i>&lt;input id="field1" type="text" class="fields" value="0"&gt;
Copy linkTweet thisAlerts:
@SiteShrineauthorMar 01.2009 — I have added the value of "0" to every field. Now I need a java that will clear the default text display when the text field is clicked on. For example Google search bars that can be placed on personal websites. The default text is can't find what you are looking for try Google or something to that effect and then you click on it and it is removed so that you can type in your search terms. That is what I am looking to do with my text fields.

Thanks
Copy linkTweet thisAlerts:
@JMRKERMar 02.2009 — How are the functions to be called?

Did you notice the order must be specific as processT1 CANNOT be called before processT2?

In the 'domath()' function, after the check for isNaN(), could you not just set the field to blank if the default value is == 0?

I'm not sure I understand the purpose of the script. Can you add some more comments?
×

Success!

Help @SiteShrine 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 6.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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...