0

I have a drodown box in my html form. I want when "0" is selected from the dropdown then the form should be reset otherwise make the ajax call to fill the form. I have written the following code for that purpose, but it is not working, I mean the form fields are not being reset.

$("select#student_id").change(function(){
  var student = $(this).val;
  if(student!=0)
    {
      //Here I make the ajax call to populate the form
    }

Here how can I reset the form fields.

1

5 Answers 5

1
$("select#student_id").change(function(){
    var student = $(this).val();
    if(student!=0) {
        //make ajax call
    } else {
        $("form").reset();
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

its not working for you because reset is not a jquery function you need to call reset function on the dom element

$('form')[0].reset()

Comments

1
$("select#student_id").change(function(){
  var student = $(this).val;
  if(student!=0)
    {
      //Here I make the ajax call to populate the form
    }
  else
  {
    var $form = $('#form_id_here');
    $form.find("input, textarea, hidden").val("").text("");
    $form.find("input[type='checkbox'], input[type='radio']").is(":checked").attr('checked', false);
  }
});

1 Comment

Not all fields are getting reset. Checkboxes are not getting reset of class toggler. Suppose some text fields come after selecting a checkbox, they are also not being reset.
0

You have to say what happens when the user selects '0'.

if(student!=0)
    {
      //Here I make the ajax call to populate the form
}
else{
 //reset form
}

Comments

0
$( "#student_id" ).change(function(event, ui){
    if( $(this).val() != 0 )
    {
      //Here I make the ajax call to populate the form
      alert("Fill the form");
    }
    else {
        //reset all the fields of the form to the default values
        $(event.target.form)[0].reset();
    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.