0
Code playground glitch?
For some reason my project keeps opening itself in the output frame when i press enter or click the button. https://code.sololearn.com/Wo717wewTt6O
1 Odpowiedź
+ 4
It's navigating to whatever the document BASE is:
alert(document.createElement("BASE").href);
(For the app it's "about:blank?input=" for the website it's the full URL)
Cause: All buttons are of type "submit".
Debugging: Here's an alert in the onclick stream:
<button onClick="alert(this.type); getInput(); ">Submit</button>
--> alerts "submit"
<button type="button" onClick="alert(this.type); getInput(); ">Submit</button>
--> FIX: alerts "button", does not submit
<input type="button" onclick="getInput()" value="Submit" />
--> FIX: may make more sense if you like it this way
********************
Role of validator
********************
For future reference, the function you call is historically supposed to be a validator, e.g.: is the form valid to submit? If not, cancel the submission.
The "validator" then is supposed to return true or false. When "submit" is the default action, you are supposed to cancel submission using a boolean "validator result":
<button onClick="getInput(); return false">Submit</button>
--> No submit: hardcoding form validation failed
<button onClick="return getInput();">Submit</button>
--> returns what getInput returns (false = validator failed, true = valid form)
There are other ways to cancel submission too (form.onsubmit function()..., etc)