Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery form reset button for all entered values

Tags:

html

jquery

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>
like image 346
NewUser Avatar asked Aug 30 '12 06:08

NewUser


People also ask

How do I clear all inputs in a form?

To clear all the input in an HTML form, use the <input> tag with the type attribute as reset.

How do I add a reset button to a form?

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.

How do I reset form value?

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.


2 Answers

Won't the <input type="reset" > suffice?

<form>

   <input type='reset'  />

</form>

DEMO

like image 145
Robin Maben Avatar answered Nov 14 '22 06:11

Robin Maben


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()

like image 45
sQVe Avatar answered Nov 14 '22 07:11

sQVe