0

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>
5
Contributors
4
Replies
13
Views
5 Years
Discussion Span
Last Post by designershiv
0

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>

Edited by Taywin: n/a

0

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);
})
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.