Sejal Patel
Sejal Patel

Reputation: 1

Clear input field without reloading of the page

I have a input field where a user enters in a name at the end of a quiz and i have tried various methods of clearing the field. The page cannot re-load as the user is able to restart a quiz.

Here is my HTML:

    <div class="js-quiz-submit">
    <input type="text" class="js-name" placeholder="Please Enter Your Name"/>
    <input type="button" class="js-submit" value="Submit"/>
    </div>

Upvotes: 0

Views: 2010

Answers (3)

HaroldV
HaroldV

Reputation: 1

Html:

<div class="js-quiz-submit">
  <input type="text" id="js-name" class="js-name" placeholder="Please Enter Your Name"/>
  <input type="button" class="js-submit" value="Submit"/>
</div>

Javascript:

document.getElementById('js-name').value='';

Upvotes: 0

user9099485
user9099485

Reputation:

I guess the better way will be :

document.querySelector(".js-name").value = "";

Upvotes: 0

Jagdeep Singh
Jagdeep Singh

Reputation: 4920

If your input field is in a form say #quiz-form, then you can clear the fields of form as follows:

var form = document.querySelector('#quiz-form');
form.reset();

Upvotes: 1

Related Questions