Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Button to multiply 1

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
I want to add a button, when someone clicks the button it should make a script prompt. The user will enter a number, say 3. Then when he/she click OK, I want it to muptiply <number> x 3600 then show the result in the text field.

How would I do that? I know absolutely nothing about JavaScript.

Thanks.
 
you will need to name your <form> tag and the <input> tag for the textfield.
Also, you will need to add onClick=&quot;caculate()&quot; to the button.
Here's a sample:

<form name=&quot;myform&quot; id=&quot;myform&quot;>
<input name=&quot;cNum&quot; id=&quot;cNum&quot; type=&quot;text&quot;>
<input type=&quot;button&quot; value=&quot;Calculate&quot; onClick=&quot;calculate()&quot;>
</form>

If you are wondering why I used both the name and the id tag. Name is used on older browsers. id is meant to replace name on newer browsers. Make both the id and name tag the same.

Now, insert the following between the two <head></head> tags:


<script language=&quot;javascript&quot;>
<!-- //hide from older browsers
function calculate() {
var textField = myform.cNum; //change to the name of your form and the name of your text field (separated by a period &quot;.&quot;)
var default_number = &quot;&quot;; //If you would like a default number to always show up in the question box, then put it inside the quotes
var new_number = prompt('Your Question', defaul_number);
new_number*=3600;
textField.value = new_number;
}
-->
</script>
 
If you wanna keep it simple just stick this on your page


<input type=&quot;text&quot; name=&quot;the_number&quot; id=&quot;the_number&quot;>
<input type=&quot;submit&quot; name=&quot;Multiply&quot; value=&quot;Multiply&quot; onClick=&quot;document.getElementById('the_number').value = parseInt(document.getElementById('the_number').value) * 3600&quot; >
 
There's a mistake in my code....

Here it is corrected:

<script language=&quot;javascript&quot;>
<!-- //hide from older browsers
function calculate() {
var textField = myform.cNum; //change to the name of your form and the name of your text field (separated by a period &quot;.&quot;)
var default_number = &quot;&quot;; //If you would like a default number to always show up in the question box, then put it inside the quotes
var new_number = prompt('Enter A Number', default_number); //replace the part in bold with your own message
new_number*=3600;
textField.value = new_number;
}
-->
</script>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top