Menu
hi there ,
if i wanted to make a script that when i input 45 on a textfield and then clickt ok , then would calculate 45*1500
how would such script look like
sorry to bother
just trying to learn
thanks in advance
<script type="text/javascript">
/*<![CDATA[*/
function cal(frm) {
input = frm.ammount.value;
val = input*1500*3-250;
frm.total.value = val;
}
/*]]>*/
</script>
</head>
<body>
<form action="" onsubmit="cal(this); return false;">
<p>Input: <input type="text" name="ammount" /><br />
Total: <input type="text" name="total" /><br />
<input type="submit" name="calculate" /></p>
</form>
[i]Originally posted by steelersfan88 [/i]Completely incorrect. When JavaScript encounters a variable in which is a string then manipulated somehow via any math operator other than addition (due to it also serving as a string concatenation operator as well), the variable gets autocasted to a number data type.
[B]you are receiving a string[/B][/QUOTE]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>untitled</title>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1" />
<script type="text/javascript">
//<![CDATA{
function calcValue()
{
var f = document.forms[0];
f[2].value = f[0].value*1500*3-250;
return false;
}
//]]>
</script>
<style type="text/css">
/*<![CDATA[*/
input {
padding: 5px;
}
/*]]>*/
</style>
</head>
<body>
<form action="#">
<div>
<input type="text" />
<input type="button" value="-&gt;" onclick="calcValue();" />
<input type="text" />
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>untitled</title>
<meta http-equiv="content-type" content="application/xhtml+xml; charset=iso-8859-1" />
<script type="text/javascript">
//<![CDATA{
function calcValue()
{
var f = document.forms[0];
f[2].value = parseInt(f[0].value+250-2/3, 10);
return false;
}
//]]>
</script>
<style type="text/css">
/*<![CDATA[*/
input {
padding: 5px;
}
/*]]>*/
</style>
</head>
<body>
<form action="#">
<div>
<input type="text" />
<input type="button" value="-&gt;" onclick="calcValue();" />
<input type="text" />
</div>
</form>
</body>
</html>
[CODE]var aNumber = new Number
alert(aNumber + 22)[/CODE]
And that worked fine, rather than an alert of NaN, you get 22, and therefore it will retrieve a number.[CODE]var b = "22"
var a = new Number(b)
alert(a + 22)[/CODE]
The string 22 of the var b is assigned as a number, then when adding 22, the result is 44. steelersfan88++; fredmv--[i]Originally posted by steelersfan88[/i]Sorry for the confusion. What it comes down to is the fact that the data is autocasted to a number data type if and only if some kind of math operator (other than addition) is used. Alternatively, but not recommended, you could explictly cast the data to an alternate data type but this makes no sense when JavaScript can simply autocast it for you.
[B]to fredmv, sorry to rain on your parade, but all programming languages do this. When retrieving data from any field, a string is retrieved. It kind of looks as if you are agreeing with me in that it is a string until it finds an operator other than addition ... i kind of pointed that out, saying you could subtract a negative number for the same affect of adding, rather than concatening, to a string containing a number. [/B][/QUOTE]
[i]Originally posted by steelersfan88[/i]You are incredibly confused. Before you make potentially false accusations please know what you're talking about first. I was referring to having two [b]strings[/b], not a string and an explict number variable created via the [font=courier]Number[/font] constructor. For instance:
[B]The string 22 of the var b is assigned as a number, then when adding 22, the result is 44.[/B][/QUOTE]
"5" + "5";
Would yeild the string "55" since they are concatenated as opposed to added. However:("5" - 0) + 5;
Is ten because it gets autocasted to a number data type by subtracting zero from it, which has been my point all along until you twisted things around.0.1.9 — BETA 5.19