0

I have problems with a simple bit of code. I'm trying to take a value from an input field and then do a simple calculation. The calculation is supposed to take place with an onSubmit command and then append it to a p tag.

HTML:

<h1 class="titleHead">Calculator</h1>

<form method="POST" action="#" onSubmit="depositCal()">
  <input type="text" name="money" id="money">
  <input type="submit" value="How much" onSubmit="">
</form>

<div>
  <p class="answer"></p>
</div>

Javascript:

$(document).ready(function() {
    var numberOne = $('#money').val(),
    numberTwo = 4;

    var finalNumber = numberOne + numberTwo;
    function depositCal() {
        $('.answer').append(finalNumber);
     }
})

I get back function not defined when it runs.

I know this is probably very simple but any help would be greatly appreciate.

4
  • 2
    Move depositCal() function outside of the $(document).ready(function(). Commented Dec 3, 2014 at 10:38
  • Declare your function outside the document.ready. You don't need it there. Commented Dec 3, 2014 at 10:38
  • If i do that i get finalNumber is not defined? Commented Dec 3, 2014 at 10:39
  • You'll also have to declare the global variables outside too (but assign their values inside) or move it all into the function Commented Dec 3, 2014 at 10:39

4 Answers 4

2

Try this:

Give your form a name and ID e.g. 'myForm'

JS

$('#myForm').submit(function(e){
     e.preventDefault();
     var numberOne = $('#money').val();
     var numberTwo = 4;
     var finalNumber = numberOne + numberTwo;
     $('.answer').append(finalNumber);
});

e.preventDefault() - stops the form from submitting (thus refreshing the page) and the function is only fired when submit is clicked.

Addition

numberOne is getting it's value from a form field so it sees it as a string. To prevent this use this line instead:

var numberOne = parseFloat($('#money').val());

Which forces the value to be a (float) number.

Sign up to request clarification or add additional context in comments.

3 Comments

Sidenote: If the Form should be submitted or the Data should be transfered elsewhere, it can be extended with $('myForm').submit() or an ajax-call after the calculation
So this works but its placing numberOne and numberTwo side by side and not adding them together?
parseFloat did it thanks everyone for your help. I'll get the hang of all this one day :)
1

You need to declare the function in global scope if you want to use it in inline js

$(document).ready(function() {
    var numberOne = $('#money').val(),
        numberTwo = 4;
    var finalNumber = numberOne + numberTwo;
})
function depositCal() {
    $('.answer').append($('#money').val() + 4);
}

You could also make it a global function by attaching the function to window object.

Comments

0

I think you don't need $(document).ready here and do calculation of finalNumber inside function so that it will give you the correct value of money input, otherwise you will get NaN or empty value-

function depositCal() {
    var numberOne = $('#money').val(),
        numberTwo = 4;
    var finalNumber = numberOne + numberTwo;
    $('.answer').append(finalNumber);
}

Comments

0

You have to keep the depositCal function definition out of $(document).ready(),because first the whole document is loaded,then $(document).ready() is called.So while loading the form,browser finds depositCal as undefined as it is defined after document is fully loaded......So keep the depositCal definition in global scope

Comments

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.