Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I send a variable from a JavaScript function to a text box? [duplicate]

Possible Duplicate:
Set the value of a input field with javascript

Okay so I am trying to get the result of my function to go back into the input box (This is part of a calculator). An example is: I enter 1 into the calculator it then does 1+2+3+4+5+6+7+8+9+10 and outputs 55 into an alert message box via the function below. I am trying to get the result to be shown back inside the box instead of an alert box.

Below I have provided the code for: The "L" function, The "input" Box, and the Button onClick that controls the function.

I have been at this for about a week trying to figure it out. If you need more code from the calculator I can provide it.

function Loop(input)
{
     var num = parseInt(input) + 10;
     var i=0;
     var sum=0;

    while(i < num)
        {
            sum=sum+i;
            i++;
        }
    alert(sum);

}


<input type="button" value="   L    " name="L" onClick="Loop(this.form.display.value)">

<form><input name="display"  id="input" size=25><form>

Here is the rest of the code seeing as the two answers I got below did not work (Thanks for trying to hep though!)

<html> 
<head>
<title>Assignment 2</title>
<SCRIPT LANGUAGE="JavaScript">
<!-- Functions
function addChar(input, character) 
{
if(input.value == null || input.value == "0")
    input.value = character;
else
    input.value += character;
}

function changeSign(input) 
{
if(input.value.substring(0, 1) == "-")
    input.value = input.value.substring(1, input.value.length);
else
    input.value = "-" + input.value;
}

function compute(form)  
{
    form.display.value = eval(form.display.value);
}

function Loop(input)
{
     var num = parseInt(input) + 10;
     var i=0;
     var sum=0;

    while(i < num)
        {
            sum=sum+i;
            i++;
        }
    document.getElementById("input").value=sum;

}
// End -->
</SCRIPT>
</head>
<body>
<div align="center">
<span style="font-weight:bold" size="20">Calculator</span>
<br>
<!-- Prints my name -->
<form  name="MyName" id="form1" style="font-weight:bold" size="20">
<script>
document.write("Mallery, Cody");
</script>
</form>
<!-- functions -->

<!-- The Calculator! -->
<center><form>
<table border=4>
<tr>
<td>
<input name="display"  id="input" size=25>
<br>
</td>
</tr>
<tr>
<td>
<input type="button" value="    7    " onClick="addChar(this.form.display, '7')">
<input type="button" value="    8    " onClick="addChar(this.form.display, '8')">
<input type="button" value="    9    " onClick="addChar(this.form.display, '9')">
<input type="button" value="    /    " onClick="addChar(this.form.display, '/')">
<br>

<input type="button" value="    4    " onClick="addChar(this.form.display, '4')">
<input type="button" value="    5    " onClick="addChar(this.form.display, '5')">
<input type="button" value="    6    " onClick="addChar(this.form.display, '6')">
<input type="button" value="    *    " onClick="addChar(this.form.display, '*')">
<br>

<input type="button" value="    1    " onClick="addChar(this.form.display, '1')">
<input type="button" value="    2    " onClick="addChar(this.form.display, '2')">
<input type="button" value="    3    " onClick="addChar(this.form.display, '3')">
<input type="button" value="    -    " onClick="addChar(this.form.display, '-')">
<br>

<input type="button" value="   0     " onClick="addChar(this.form.display, '0')">
<input type="button" value="    N    " onClick="changeSign(this.form.display)">
<input type="button" value="    +    " onClick="addChar(this.form.display, '+')">
<input type="button" value="   C   " onClick="this.form.display.value = 0 ">
<br>
<input type="button" value="   L    " name="L" onClick="Loop(this.form.display.value)" 
                                               title="If the L button is pressed, the digit present in the results box will be looped through and added up to the 'digit plus 10'.For example: After the calculator has been reset. The user can press the 1 button, then the L button 55 should be displayed in the calculator 1 + 10 = 11, therefore start with 1 and loop until less than 11 adding all of the numbers 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55">
<input type="button" value="   =    " name="enter" onClick="compute(this.form)">
</td>
</tr>
</table>
like image 328
Cody Mallery Avatar asked Mar 06 '26 21:03

Cody Mallery


1 Answers

Put this in line in your JS function instead of your alert

document.getElementById("input").value=sum;
like image 174
polin Avatar answered Mar 09 '26 09:03

polin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!