0

I have written some code to remove the input placeholder text and it works as expected. The problem I have is how do I reset the value of placeholder after it has been removed?

const input = document.getElementById('input'); if (input.placeholder) { input.addEventListener('focus', (e) => { input.placeholder = ''; }); } else { input.placeholder.preventDefault() };
<div class="mainInput"> <input type="text" class="input" id="input" placeholder="Enter your todo : "> <button class="btn" id="btn">Submit</button> </div> <span> Todo list : </span>

5
  • 5
    What do you mean by "reset"? Why do you remove the placeholder in the first place? Commented Dec 21, 2020 at 12:47
  • use remove attribute function on load : input.removeAttribute("placeholder"); Commented Dec 21, 2020 at 12:48
  • @NicoHaase Yes there is no need to remove the placeholder. Commented Dec 21, 2020 at 12:49
  • 2
    By default any placeholder text is removed as soon as you start typing in the input - when you clear the input it is "reset" (placeholder text reappears). I am really not sure what you are trying to achieve here beyond the default behaviour Commented Dec 21, 2020 at 12:50
  • 1
    if you just want to hide the placeholder then this is the solution you are looking for stackoverflow.com/questions/9707021/… Commented Dec 21, 2020 at 12:57

1 Answer 1

0

I think this solution should work for you!

It will just reset your placeholder value.

const input = document.getElementById('input'); if (input) { // it will remove placeholder value on focus and store with dataset variable input.addEventListener('focus', (e) => { input.dataset.placeholder = input.placeholder input.placeholder = ''; }); // it will reset placeholder value on blur by dataset variable input.addEventListener('blur', (e) => { input.placeholder = input.dataset.placeholder; }); }else{ console.log("input is not defined",input); }
<div class="mainInput"> <input type="text" class="input" id="input" placeholder="Enter your todo : "> <button class="btn" id="btn">Submit</button> </div> <span> Todo list : </span>

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.