1

I'm working on a login/register page, in which the forms switch depending on whether the user decide to login or register.

I'm trying to empty all of the <input> when the user switch forms.

I can get to erase all the contents of the <input> fields, but that's including the <input type="submit">, thus also erasing the submit button's message.

Here's what I tried to do to select every input except the submit one, but it doesn't empty the fields anymore :

$('.message a').click(function(){
      $('form').animate({height: "toggle", opacity: "toggle"}, "slow");
      $(':text, :password, :email').each(function(){
            $(this).val('');
      });
});

And here's what I tried before, selecting all input, also erasing the "submit" one :

$('.message a').click(function(){
      $('form').animate({height: "toggle", opacity: "toggle"}, "slow");
      $('input').each(function(){
            $(this).val('');
      });
});

Also, here's the html :

<div class="form">
    <form class="register-form" autocomplete="off">
      <input type="text" placeholder="nom" name="name_register"/>
      <input type="text" placeholder="prenom" name="last_name_register"/>
      <input type="password" placeholder="mot de passe" name="password_register"/>
      <input type="email" placeholder="adresse email" name="email_register"/>
      <input type="submit" value="Inscription"/>
      <p class="message">Already registered? <a href="#" class="register">Sign In</a></p>
    </form>
    <form class="login-form">
      <input type="email" placeholder="adresse email" name="email_login"/>
      <input type="password" placeholder="mot de passe" name="password_login"/>
      <input type="submit" value="Connexion"/>
      <p class="message">Vous n'êtes pas enregistré? <a href="#" class="login">Créer un compte</a></p>
    </form>
  </div>
1
  • Don't use input tag for submit button, use button tag instead: <button type="submit">Connexion</button> Commented Mar 13, 2017 at 8:58

5 Answers 5

2
$("input:not([type='submit'])").each

https://api.jquery.com/not-selector/

Sign up to request clarification or add additional context in comments.

Comments

2

You should use this selector:

$("input:not([type=submit])")

1 Comment

Use single quotes, your double quotes are escaping.
1

User the following instead.

$('.message a').click(function(){
      $('form').animate({height: "toggle", opacity: "toggle"}, "slow");
      $('input[type=text], input[type=password], input[type=email]').each(function(){
            $(this).val('');
      });
});

Comments

1

try $('.register-form')[0].reset(); it will reset form values

Comments

0

if i understand your requirments correctly following will help you

$('.message a').click(function() {
  $('form').animate({
    height: "toggle",
    opacity: "toggle"
  }, "slow");
  $('input[type=text], input[type=email], input[type=password]').each(function() {
    $(this).val('');
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

 

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.