Link to home
Start Free TrialLog in
Avatar of datzent83
datzent83Flag for United States of America

asked on

Javascript Multiply TextBox with a Hidden Number - Very Simple

Hello Experts,

I need to multiply a number from a textbox by a given number, click on a "GO" button and then display the answer in another textbox. I need the number that is being multiplied by to be hidden.

I need this to be done ASAP, that is why I give this question 500 points.

Thank you all in advance!

Igor
Avatar of Manish
Manish
Flag of India image

So what you have done so far?
<html>
<head>
<title>test</title>
<script>
function mult (input) {
      var secret_number=42;
      document.form.answer.value = (input * secret_number)
}
</script>
</head>
<body>
<form name='form' action="javascript:mult(document.form.input.value)>
<input type='text' name='input'><br>
<input type='text' name='answer'><br>
<input type='submit'>
</body>
</html>
Type in the 1st box your number and then click the button.  Answer in 2nd box.  Just a quick example...

Can do more to make it look better.  But this will get the job done.
Avatar of datzent83

ASKER

The code works perfectly! Thank you very much!

Instead of a button, can we create a link?
Link to go where?  Or do you want "CLICK HERE TO SEE..."
ASKER CERTIFIED SOLUTION
Avatar of radnor
radnor
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That's it! That's exactly what I was looking for !

Thank you very much rodnor. Point well earned! :)
Hmmmm! It looks like I missed something.

I forgot to mention that there will be multiple calculators like this on one page. It seems there are conflicts with form names.

Can we rewrite the code to use multiple forms on one page?

Please help!
How many?  Will you answer all at once or progress through them?  If you answer all at once, can add the additional text boxes and change the function a little.

Spill it, what EXACTLY do you need?
I have a product page, the product page has 6-10 products. Under each product I need a calculator to calculate how much of something they need.

So I need to have a calculator for each product.

Any suggestions?
Well....  Still need little more  info but with the info given...  This should work for the 1st 4.  You can see what I am doing.

If the number is the same (secret_number), then I'd just add more input fields and mod the function.  You also said 6-10 products.  I would create it for 10 and if less than than on a page, make the INPUT box HIDDEN.   I am assuming 6-10 products for up to X pages????

<html>
<head>
<title>test</title>
<script>
function mult () {
      var secret_number=42;
      document.form.answer1.value = (document.form.input1.value * secret_number)
      document.form.answer2.value = (document.form.input2.value * secret_number)
      document.form.answer3.value = (document.form.input3.value * secret_number)
      document.form.answer4.value = (document.form.input4.value * secret_number)
}
</script>
</head>
<body>
<form name='form'>
<input type='text' name='input1'><br>
<input type='text' name='answer1'><br>
<input type='text' name='input2'><br>
<input type='text' name='answer2'><br>
<input type='text' name='input3'><br>
<input type='text' name='answer3'><br>
<input type='text' name='input4'><br>
<input type='text' name='answer4'><br>
<a href="#" onClick="javascript:mult()">Click here to see your answer...</a>
</body>
</html>
We are almost there, but not exactly what I need. I revised your form a little bit to show you how I need it to be done.

I need each calculator to be multiplied by a different number. Please take a look at the code to see what I mean.

<html>
<head>
<title>test</title>
<script>
function mult () {
      var secret_number=42;
      document.form.answer1.value = (document.form.input1.value * secret_number)
      document.form.answer2.value = (document.form.input2.value * secret_number)
      document.form.answer3.value = (document.form.input3.value * secret_number)
      document.form.answer4.value = (document.form.input4.value * secret_number)
}
</script>
</head>
<body>
<form name='form'>
<input type='text' name='input1'><br>
<input type='text' name='answer1'> x .03<br>
<a href="#" onClick="javascript:mult()">Click here to see your answer...</a>
<p>
<input type='text' name='input2'><br>
<input type='text' name='answer2'> x .05<br>
<a href="#" onClick="javascript:mult()">Click here to see your answer...</a>
</p>
<p>
<input type='text' name='input3'><br>
<input type='text' name='answer3'> x.02<br>
<a href="#" onClick="javascript:mult()">Click here to see your answer...</a>
<br>
<br>
<input type='text' name='input4'><br>
<input type='text' name='answer4'> .06<br>
<a href="#" onClick="javascript:mult()">Click here to see your answer...</a>
</p>
</body>
</html>
<html>
<head>
<title>test</title>
<script>
function mult () {

      document.form.answer1.value = (document.form.input1.value * .03)
      document.form.answer2.value = (document.form.input2.value * .05)
      document.form.answer3.value = (document.form.input3.value * .02)
      document.form.answer4.value = (document.form.input4.value * .06)
}
</script>
</head>
<body>
<form name='form'>
<input type='text' name='input1'><br>
<input type='text' name='answer1'> x .03<br>
<a href="#" onClick="javascript:mult()">Click here to see your answer...</a>
<p>
<input type='text' name='input2'><br>
<input type='text' name='answer2'> x .05<br>
<a href="#" onClick="javascript:mult()">Click here to see your answer...</a>
</p>
<p>
<input type='text' name='input3'><br>
<input type='text' name='answer3'> x.02<br>
<a href="#" onClick="javascript:mult()">Click here to see your answer...</a>
<br>
<br>
<input type='text' name='input4'><br>
<input type='text' name='answer4'> .06<br>
<a href="#" onClick="javascript:mult()">Click here to see your answer...</a>
</p>
</body>
</html>
All the calculators are part of the same form. Can we divide the calculators into different forms? Each calculator will be in a different table.
OK, OK, try this.  Let me know if this will work for you.  What you are doing is passing the function 2 variables.  The 1st is the RATE and the 2nd is the NUMBER IN SINGLE QUOTES 1 to 10 for your products.  The function will then build a string and get the value of that input box, do the calculation, and write the answer to the correct output box.

Hope this helps!
I am not much of a javascript programmer, but below is what I got. However, its complaining about form2. It calculates form1 but then it dies at form2.

<html>
<head>
<title>test</title>
<script>
function mult () {

      document.form1.answer1.value = (document.form1.input1.value * .03)
      document.form2.answer2.value = (document.form2.input2.value * .05)
      document.form3.answer3.value = (document.form3.input3.value * .02)
      document.form4.answer4.value = (document.form4.input4.value * .06)
}
</script>
</head>
<body>
<form name='form1'>
<input type='text' name='input1' size="5" style="border: 1px solid #999999">
<a href="#" onClick="javascript:mult()">GO</a>
<input type='text' name='answer1' size="5" style="border: 1px solid #999999"> <br>
&nbsp;<p>
</form>

<form name='form2'>
<input type='text' name='input2' size="5" style="border: 1px solid #999999">
<a href="#" onClick="javascript:mult()">GO</a>&nbsp;
<input type='text' name='answer2' size="5" style="border: 1px solid #999999"> <br>
&nbsp;</p>
<p>
</form>

<form name='form3'>
<input type='text' name='input3' size="5" style="border: 1px solid #999999">
<a href="#" onClick="javascript:mult()">GO</a>&nbsp;
<input type='text' name='answer3' size="5" style="border: 1px solid #999999"> <br>
<br>
<br>
</form>

<form name='form2'>
<input type='text' name='input4' size="5" style="border: 1px solid #999999">
<a href="#" onClick="javascript:mult()">GO</a>&nbsp;
<input type='text' name='answer4' size="5" style="border: 1px solid #999999">
</p>
</form>
</body>
</html>
I am very sorry!  I'm glad I came back to see if you posted anything more on this question.  I forgot to include the code.  Here is where it takes 2 parameters as stated above.  I think you'll like this.  Would like to see a link sometime.  I work on sites and have a few I did and maintain.  Good luck and have a great weekend!

<html>
<head>
<title>test</title>
<script>
function mult (secret_number, form_input) {
      input = eval("document.form.input"+form_input);
      output = eval("document.form.answer"+form_input);
      output.value = (input.value * secret_number);
}
</script>
</head>
<body>
<form name='form'>
<input type='text' name='input1'><br>
<input type='text' name='answer1'> x .03<br>
<a href="#" onClick="javascript:mult(.03, '1')">Click here to see your answer...</a>
<p>
<input type='text' name='input2'><br>
<input type='text' name='answer2'> x .05<br>
<a href="#" onClick="javascript:mult(.05, '2')">Click here to see your answer...</a>
</p>
<p>
<input type='text' name='input3'><br>
<input type='text' name='answer3'> x.02<br>
<a href="#" onClick="javascript:mult(.02, '3')">Click here to see your answer...</a>
<br>
<br>
<input type='text' name='input4'><br>
<input type='text' name='answer4'> .06<br>
<a href="#" onClick="javascript:mult(.06, '4')">Click here to see your answer...</a>
</p>
</body>
</html>
Hi There,

I can't officially release the link to the website as this website is a proposal for a client of mine. What you have is almost exactly what I need. However, the code for each calculator still falls into one form.

Below is what I need to accomplish. As you will see, each calculator is coded in its own form, not into one large form. This way I can move each calculator between tables, cells, or divs.

<form name='form'>
<input type='text' name='input1'><br>
<input type='text' name='answer1'> x .03<br>
<a href="#" onClick="javascript:mult(.03, '1')">Click here to see your answer...</a>
<p></form>

<form name="form2">
<input type='text' name='input2'><br>
<input type='text' name='answer2'> x .05<br>
<a href="#" onClick="javascript:mult(.05, '2')">Click here to see your answer...</a>
</p>
<p>
</form>

<form name="form3">
<input type='text' name='input3'><br>
<input type='text' name='answer3'> x.02<br>
<a href="#" onClick="javascript:mult(.02, '3')">Click here to see your answer...</a>
<br>
<br>
</form>

<form name="form4">
<input type='text' name='input4'><br>
<input type='text' name='answer4'> .06<br>
<a href="#" onClick="javascript:mult(.06, '4')">Click here to see your answer...</a>
</p>
</form>
form 1 si not working, correct?  how about the others?  I see right off the top, an error in form(1)
The code from my last post is just the way I need the code to be formed. It is not a solution to what i need. :)
That's fine, but the ERROR is here:
<form name='form'>  <<<**********************************
<input type='text' name='input1'><br>
<input type='text' name='answer1'> x .03<br>
<a href="#" onClick="javascript:mult(.03, '1')">Click here to see your answer...</a>
<p></form>

This will work:
function is:
<script>
function mult (secret_number, form_input) {
      input = eval("document.form"+form_input+".input"+form_input);
      output = eval("document.form"+form_input+".answer"+form_input);
      output.value = (input.value * secret_number);
}
</script>

and CHANGE your 1st form NAME FROM "form" to "form1"  This is where you had an error.  Want full code, let me know.
Full code follows:

<html>
<head>
<title>test</title>
<script>
function mult (secret_number, form_input) {
      input = eval("document.form"+form_input+".input"+form_input);
      output = eval("document.form"+form_input+".answer"+form_input);
      output.value = (input.value * secret_number);
}
</script>
</head>
<body>
<form name='form1'>
<input type='text' name='input1'><br>
<input type='text' name='answer1'> x .03<br>
<a href="#" onClick="javascript:mult(.03, '1')">Click here to see your answer...</a>
<p></form>

<form name="form2">
<input type='text' name='input2'><br>
<input type='text' name='answer2'> x .05<br>
<a href="#" onClick="javascript:mult(.05, '2')">Click here to see your answer...</a>
</p>
<p>
</form>

<form name="form3">
<input type='text' name='input3'><br>
<input type='text' name='answer3'> x.02<br>
<a href="#" onClick="javascript:mult(.02, '3')">Click here to see your answer...</a>
<br>
<br>
</form>

<form name="form4">
<input type='text' name='input4'><br>
<input type='text' name='answer4'> .06<br>
<a href="#" onClick="javascript:mult(.06, '4')">Click here to see your answer...</a>
</p>
</form>
</body>
</html>
THATS IT!!! YOU GOT IT! :) :) :)
Thank you very much for this. This is exactly what I was looking for! I will release the link for you when the project is officially announced a "Go".

Thanks Again!
COOL!  Did you see the error?  You had form and NOT form1.  Missing the 1 (one).  Other than that, you would have had it.  Sometimes a different pair of eyes will pick these things up quickly.  Good luck with the project!
I noticed the error when you pointed it out. Having a second pair of eyes is always a good idea in a project of this sort.

Thanks!