I want to clear my text from an input in html input, but it doesn’t work.
I tried this but it shows up with ‘error 404, page does not exist’ when I click submit.
<sub> <h3>
<hr>
Tell me what <strong>your</strong> favorite colours is!
</h3>
<form name='cols'>
<input type='text' id='col1'>
<input type='text' id='col2'>
<input type='text' id='col3'>
<input type="submit" value="Submit" id="submit" onclick="clear()">
<script>
function clear() {
var frm = document.getElementsByName('cols');
frm.reset();
return false;
}
</script>
</form>
</body>
</sub>
2
Answers
If you want to reset when you click the submit button change the name to id in form then just change the function into this:
The reason you encounter a 404 error is because the browser was actually trying to submit the
<form>
to a server, which is the expected behaviour for asubmit
<button>
(it is preferable to use<button type="submit">
over<button type="submit">
, even if they act the same way).The server URL it tried to submit the form is the same as the one of the current page by default, unless you explicitly specify an action for the form. For example:
If you want to reset the form without submitting any data, you don’t need any JavaScript: instead, you can use the native HTML reset button: