+ 3
Why is the final alert box not showing?
On this code https://code.sololearn.com/WPgR6bFu0tEo/?ref=app The final alert box is not showing (the one that says correct or incorrect)
2 Respostas
+ 1
Fix:
Inside the html tab remove the form element and keep everything else.
For instance:
<input type="number" name="form1">
<input type="submit" onclick="submit()">
And in the JavaScript tab inside the submit function do the following:
Change the value of the "form1" variable to [parseInt(document.getElementsByName("form1")[0].value)],
Reduce the following in your "if" statement from "form1.form1" to "form1"
example code:
https://code.sololearn.com/WmmNTRAp9c7W/?ref=app
+ 1
Explanation:
it's because your input elements are inside of a form tag, when you submit a form, the form doesn't get submitted to your JavaScript script it gets submitted to whatever the "action" attribute is pointing at(usually a PHP file to process the submitted data), in your case the "action" attribute is pointing at nothing.
In general trying to submit your code in "web" codes just breaks your code unless you're submitting to a server, your code will not work.
If you want to get user input via input element then just put your input elements without a form tag and give them an id to reference and use their values in JavaScript.
For instance:
Html
<input type="number" id="any_name_you_want">
JavaScript
var myNumber = document.getElementById("any_name_you_want").value
And of course you can reference the "name" attribute and the "class" attribute in your JavaScript with other methods.
All this can be found here in the sololearn JavaScript and html course please study your fundamentals well.