New to web developing, got an error (jsfiddle)

100 views Asked by At

I am an absolute begginer to web developing and I want to simply do a chance calculator in the online tool: jsfiddle. I got the error:"Shell form does not validate" and some strange errors after. Here is my HTML code:

<body>
    <form method="post">The chance of succes:
        <input type="number" name="chance">
        <br>How many tries:
        <input type="number" name="tries">
        <br>
        <input type="submit" value="Calculate!">
        <br>
    </form>
</body>

And my javascript code is:

function calculate(chance, tries) {
    console.log(chance / tries * 100);
}

As I said, I am new to this so please try to explain step by step.

1

There are 1 answers

2
John Stringer On BEST ANSWER

You have a few problems. One is that your form is trying to submit itself to itself. Another is that your calculate function is never called.

Your parameters for the function are never called either. Let's change the inputs to have an id instead of a name. For example:

<input type="number" id="chance">

This makes it easier to get the value from the input when clicking the button. I've made the input a button instead of a submit, just to make sure that your form data isn't getting sent anywhere.

<input type="button" onclick="return calculate(getElementById('chance').value, getElementById('tries').value)" value="Calculate!">

Here is a jsfiddle with a possible solution for you. http://jsfiddle.net/0dmkxcab/