1

How can I use the value from a function in an if statement. I have a form where I was using return false in my script but I need changed it to preventDefault.

<form id="percentageBiz" method="post">
<input type="text" id="sum1">
<input type="text" id="sum2">
<input type="submit" onclick="return" value="Get Total">
</form>
<div id="display"></div>​

<script>
$('#percentageBiz').submit(function(e) {
    var a = document.forms["percentageBiz"]["sum1"].value;
    var b = document.forms["percentageBiz"]["sum2"].value;
    var display=document.getElementById("display")
    display.innerHTML=parseInt(a,10)+parseInt(b,10);
    e.preventDefault();
});
if (display < 100) {
    $("#display").addClass("notequal");
}
</script>
2
  • try preventDefault at the very top. Commented Jan 7, 2013 at 2:00
  • I added preventDefault to the very top do I now add return display; to the end of the function? I tried it and I still can't seem to retrieve the value of the function. Commented Jan 7, 2013 at 2:31

1 Answer 1

1
$('#percentageBiz').submit(function(e) {
    e.preventDefault();

    var $display = $("#display", this);
    var a = $("#sum1", this).val();
    var b = $("#sum2", this).val();
    var sum = +a + +b;

    $display.text( sum );

    if ( sum < 100 ) {
        $display.addClass("notequal");
    }
});
Sign up to request clarification or add additional context in comments.

1 Comment

The above didn't seem to work. Do you still need parseInt of the variables a and b?

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.