I am using a form and a reset button where I can reset all the entered values but it is not working at all.
When I debugged with the Firefox console tab, I saw an error illegal character
at the end of the jQuery script. Can someone tell me what is the wrong part here?
<!DOCTYPE HTML>
<html lang="en-IN">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="js/jquery1.7.2.js"></script>
</head>
<body>
<script type="text/javascript">
jQuery('#reset').click(function(){
$(':input','#myform')
.not(':button, :submit, :reset, :hidden')
.val('')
.removeAttr('checked')
.removeAttr('selected');
});
</script>
<form id='myform'>
<input type='text' value='test' />
<select>
<option>One</option>
<option selected="true">Two</option>
</select>
<select multiple="true" size="5">
<option>One</option>
<option selected="true">Two</option>
</select>
<input type='button' id='reset' value='reset' />
</form>
</body>
</html>
To clear all the input in an HTML form, use the <input> tag with the type attribute as reset.
Type the <input type="reset"> tag into the code towards the top and/or bottom of the HTML form. Close the form after all input fields are entered with a final </form> tag. Save and preview your new adjusted form with the new reset button.
reset() method restores a form element's default values. This method does the same thing as clicking the form's <input type="reset"> control. If a form control (such as a reset button) has a name or id of reset it will mask the form's reset method.
Won't the <input type="reset" >
suffice?
<form>
<input type='reset' />
</form>
DEMO
Try the following:
$(function() {
$('#reset').click(function() {
$(':input','#myform')
.not(':button, :submit, :reset, :hidden')
.val('')
.removeAttr('checked')
.removeAttr('selected');
});
});
You're running the javascript before the DOM is ready. $(function() {})
only runs when the DOM is ready. Read more here: .ready()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With