3

I'm trying to get the textfields to return to their default value when the user clicks the Reset button.

All it does now when the user clicks the Reset button is replacing the user's text with ''.

How can I do it by using pure JavaScript (no jQuery)?

HTML:

<p>Type the first number</p>
<input id="first" type="text" placeholder="First Number" />
<p>Type the second number</p>
<input id="second" type="text" placeholder="Second Number" />
<button id="aButton">Apply</button>
<button id="rButton">Reset</button>
<div id="add"></div>

JAVASCRIPT:

app.onactivated = function (args) {
        var aButton = document.getElementById("aButton");
        aButton.addEventListener("click", buttonClickHandler, false);

        var rButton = document.getElementById("rButton");
        rButton.addEventListener("click", buttonResetHandler, false);

    };

...

function buttonResetHandler(evetInfo) {

        document.getElementById("first").innerText = '';
        document.getElementById("second").innerText = '';

    }
1
  • <button type="reset">Reset</button> Commented May 26, 2013 at 23:30

4 Answers 4

10

innerText is an invalid property that is implemented in IE browsers and is used for setting/getting text content of non-form elements, if the values should be set as default, you can use defaultValue property:

var a = document.getElementById("first"),
    b = document.getElementById("second");
a.value = a.defaultValue;
b.value = b.defaultValue;

If you want to reset all the form elements, you can use .reset() method of DOM HTMLFormElement object:

document.forms["myForm"].reset();
Sign up to request clarification or add additional context in comments.

Comments

2
  1. location.reload(); // reloads the page
  2. history.go(0); // deletes the history
  3. But if you need to preserve some values inside of the page then reassign the values in the function again. To reassign, write the variable (declare in let to change later) again in the function and change the textContent again.

Comments

0

Replace,

document.getElementById("first").innerText

With,

document.getElementById("first").value

Example:

<input id="txtBox" type="text" value="lama">
<input type="button" value="reset lama" onclick='document.getElementById("txtBox").value="lama2";'>

1 Comment

add to the button the event. <button id="resetBtn" onclick='buttonResetHandler();'>Reset</button>
-1

create an init function that sets the default values for each input, then you can call that:

function initializeInputs() {
    document.getElementById("first").value = '';
    document.getElementById("second").value= '';
}

function buttonResetHandler(e) {
    initializeInputs();
}

2 Comments

document.getElementById("first").innerText gives an error. And I think thats not what he asked.
The above just replaces the user's input with something else. What I want is to remove the user's input and restore the default value (First Number/Second Number).

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.