i wanna make simple script that multiply any input by .51 and show the result automatic, without pressing any button,i got two scripts first script add value one to value two and show result auto, other script multiply but i have to press button, how to merge them :S
and if there are script do the job better than those tell me :)
Target: Input Box, when i input any value the result of multiply that value by .51 appear beside it automatic :)

Script One:

</head>
<script type="text/javascript" language="javascript">

function autocalc(oText)
{
	if (isNaN(oText.value)) //filter input
	{
		alert('Numbers only!');
		oText.value = '';
	}
	var field, val, oForm = oText.form, total = a = 0;
	for (a; a < arguments.length; ++a) //loop through text elements
	{
		field = arguments[a];
		val = parseFloat(field.value); //get value
		if (!isNaN(val)) //number?
		{
			total += val; //accumulate
		}
	}
	oForm.total.value = total; //out
}
		

</script>
</head>

<body  onload="document.forms[0].reset()">
<form style="width:260px;margin:100px auto;border:2px black dashed;">
<table cellspacing="8">
<tr>
<!-- pass field reference ('this') and other field references -->
<td>value 1___<input name="t1" type="text" onkeyup="return autocalc(this,t2)" tabindex="1"></td>
<td rowspan="3"><strong>total_____</strong><input name="total" type="text" readonly="readonly" value="0"  tabindex="-1"></td>
</tr><tr>
<td>value 2___<input name="t2" type="text" onkeyup="return autocalc(this,t1)" tabindex="2"></td>
</tr>
</table>
</form>
</body>

Code Two:

<head>
<script language="JavaScript">



<!-- Begin
function  doMath() {
var one = eval(document.theForm.elements[0].value)
var two = eval(document.theForm.elements[1].value)
var prod = one  *   two
alert("The product of "  +  one  +  " and "  +
two  +  " is "  +  prod  +  ".")
}
// End -->
</script>
</head>
<body>
<center>
<form name="theForm">
Multiply
<input type="text">
 By
<input type="text">
<input type="button" value="Show result" onClick="doMath()">
</form>
</center>
</body>

Recommended Answers

All 4 Replies

I didn't understood what you want.

Are you talking about multiplying by 0.51 after the total value is computed (added 2 values)? Where do you want the output to be in your page? Different element?

I can see that the way you call the function & the implementation are not going well together. If you explicitly accept 1 argument, why do you need to loop through arguments? Also, you are using variable 'a' as a global. It is dangerous.

The code below is an example. You may need to modify to your need.

<html>
<head>
  <script type="text/javascript">
  function compute(inputObj, otherInputID, addResultID, mulResultID) {
    var otherObj = document.getElementById(otherInputID)
    var addResultObj = document.getElementById(addResultID)
    var mulResultObj = document.getElementById(mulResultID)

    if (inputObj && otherObj) {  // both inputs exist
      var v1=inputObj.value
      var v2=otherObj.value

      // error checking allows empty string value as 0
      if (v1!="" && isNaN(v1)) {  // error checking for inputObj value
        alert("Number only!")
        inputObj.value = ""
        return  // break from the script
      }
      if (v2!="" && isNaN(v2)) {  // error checking for otherObj value
        alert("Number only!")
        otherObj.value = ""
        return  // break from the script
      }

      var val1 = v1=="" ? 0 : parseFloat(v1)  // convert string to float
      var val2 = v2=="" ? 0 : parseFloat(v2)  // convert string to float
      if (addResultObj) {  // display if the added result object exists
        addResultObj.value = val1 + val2
      }

      if (mulResultObj) {  // display if the multiplied result object exists
        mulResultObj.value = (val1+val2)*0.51
      }
    }
  }
  </script>
</head>

<body>
  Value 1: <input id="txt1" type="text" onkeyup="compute(this, 'txt2', 'addres', 'mulres')">
  <br />
  Value 2: <input id="txt2" type="text" onkeyup="compute(this, 'txt1', 'addres', 'mulres')">
  <br />
  Added Result: <input id="addres" type="text">
  <br />
  Multiplied Result: <input id="mulres" type="text">
</body>
</html>

but how can you add Cash and change and quantity left in this code?

Hi DarkBerzerk,

I think this what you want, Please check the code and let me know whenther it solves your problem

$('input').keyup(function(){
    var getValue = $(this).val().length;
    $('.resultdiv').html(getValue*51);
})
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.