0

I have a script, where I am trying to access a variable and then have a text field, where I am able to input numbers and then when the "submit" button is clicked, I want the "input" to be added to the variable, the only problem is my code for displaying the variable won't work.

my code(html):

    <form id="form" onsubmit="return false;">
    <input style="width:100px;" type="text" id="Input" />
    <input style="width: 100px;" type="submit" id="submitBut" onclick="getInput();" />
</form>

<a href="#">current amount is: <span id="amountSoFar"></span></a>

my code(javascript):

     var amountSoFar : int = 1;

function getInput() {
    var input = document.getElementById("Input").value;
    amountSoFar = amountSoFar + input;
    alert(input);
}

my code(css):

 #Input{
    position: absolute;
    top: 100px;
    margin-left: -100px;
    left: 50%;
}

#submitBut{ 
    position: absolute;
    top: 120px;
    margin-left: -100px;
    left: 50%;    
}
1
  • what's this ?!!! var amountSoFar : int = 1; Commented Mar 17, 2015 at 18:38

3 Answers 3

2

This line is puzzling:

 var amountSoFar : int = 1;

If you are trying to declare a type, it's not necessary. Javascript will infer that it's a number based on the assigned value. You could just do:

var amountSoFar = 1;
Sign up to request clarification or add additional context in comments.

Comments

0
var amountSoFar = 1;

$('#form').submit(function (event) {
    event.preventDefault();
    var input = $('#Input').val();
    amountSoFar = parseInt(amountSoFar) + parseInt(input);
    $('#amountSoFar').text(amountSoFar);
});
#Input {
    position: absolute;
    top: 100px;
    margin-left: -100px;
    left: 50%;
}
#submitBut {
    position: absolute;
    top: 120px;
    margin-left: -100px;
    left: 50%;
}
<form id="form" onsubmit="return false;">
    <input style="width:100px;" type="text" id="Input" />
    <input style="width: 100px;" type="submit" id="submitBut" />
</form>
<a href="#">current amount is: <span id="amountSoFar"></span></a>

CSS and JS is the same as your original example. The JS was changed to be triggered on $('#form').submit() so that it worked with the enter key as well. It was also updated to update the live value on the page.

Working JS Fiddle: http://jsfiddle.net/mb1sd8c7/

2 Comments

It works, but why does it only show the variable number when it's above 1, when nothing has been done to it, it doesn't show the number but then when you submit a number, it changes and you are able to see the number.
You can put a starting number in there, or setup the javascript to initialize it if you wanted. There is no starting number in my example because there wasn't one in yours.
0

change

 var amountSoFar : int = 1;

to

 var amountSoFar , int = 1;

or

var amountSoFar;
var int = 1;

Here is a demo

var amountSoFar , int = 1;

function getInput() {
    var input = document.getElementById("Input").value;
    amountSoFar = amountSoFar + input;
    alert(input);
}
 #Input{
    position: absolute;
    top: 100px;
    margin-left: -100px;
    left: 50%;
}

#submitBut{ 
    position: absolute;
    top: 120px;
    margin-left: -100px;
    left: 50%;    
}
    <form id="form" onsubmit="return false;">
    <input style="width:100px;" type="text" id="Input" />
    <input style="width: 100px;" type="submit" id="submitBut" onclick="getInput();" />
</form>

<a href="#">current amount is: <span id="amountSoFar"></span></a>

2 Comments

I think ":int" is just an attempt to specify a type. I suspect he meant to say amountSoFar = 1 since "int" is not being used.
@brianvaughn but in javascript there is not such a type

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.