1

I am trying to reset an input field using Java Script and it does not work in the way I intend to. I am trying to undestand why it works in a certain way and not in another way. Could you please explain why and provide any pointers if possible. Thank you.

Input text field I am trying to reset:

<input type="text" id="textInput" class="inp">

I defined a button like this:

<button id="resetButton">Reset</button>

I created an event listener on the reset button and used it like below.

var rButton=document.getElementById("resetButton");

rButton.addEventListener("click", function() {
    var inputTextValue=document.getElementById("textInput").value;
    inputTextValue="";
});

This is the JS code I am getting the value from the input text:

 var inputTextValue=document.getElementById("textInput").value;

And to reset the value, I am just assigning it to empty string:

inputTextValue="";

This piece of code doesn't work but when I try to get the reference of the input text like this -

var inputText=document.getElementById("textInput");

and try to reset the value like this, it works -

inputText.value="";

So, I am trying to understand, what is the difference in both ways and what am I doing wrong.

2
  • You need to directly set the .value property to "". Commented Oct 4, 2018 at 20:08
  • You're essentially doing a get when you want to do a set Commented Oct 4, 2018 at 20:10

3 Answers 3

4

When you assign the value property to a variable, that's exactly what you're doing. You're simply assigning the value, not creating a reference - either set the entire element to the variable and update the value property, or just update the value property at once:

document.getElementById("textInput").value = "";

Or:

var input = document.getElementById("textInput");
input.value = "";
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for your reply! Your first sentence makes sense. This could be marked as a correct answer, but since I do not have enough reputation, I couldn't do so.
2

You are copying the current value of your input field into the inputTextValue string and then you replace that copy with an empty string. That means you are not actually altering your input field.

Try

document.getElementById("textInput").value = "";

or

 var input = document.getElementById("textInput");
 input.value = "";

instead.

1 Comment

Understood. Thanks for your helpful inputs!
1

This way you can reset or select.

$(document).on("click", "#resetButton", function() {
    var textInputValue = $("#textInput").val(); // get value
    $("#textInput").val(""); // or reset value
});

or

var input = document.getElementById("textInput");
var resetButton = document.getElementById("resetButton");

resetButton.addEventListener("click", function() {
    input.val(); // get value
    input.val(""); // or reset value
});

Edit: I haven't seen the Javascript tag. Sorry. :)

1 Comment

Thank for the solution, but I am not using jQuery! Thanks for the solution though!

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.