Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

288
Views
JavaScript (eventlistner or something)

I have a simple html with a input box(id=input) and a button(id=button) with 'Show' as button's text. I have wrote JavaScript to change show to hide on click and basic stuff.

Initinally , no button is displayed (as button display is none). What I want is as soon as I type anything in the input box Show button should display. How to do so in JavaScript?

Here is my JavaScript:-

let input = document.getElementById('input');
let btn = document.getElementById('button');

btn.addEventListener('click', () => {
    if (btn.innerText === "SHOW") {
        btn.innerText = "HIDE";
        input.type = "text";
    } else {
        btn.innerText = "SHOW";
        input.type = "password";
    } 
})
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

The below snippet shows and hides button according to the data that is inside the input

Also this is the event you were looking for https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/input_event

document.querySelector("#pass").addEventListener('input', 
(event) => 
  {
    if (document.querySelector("#pass").value.length === 0 ) {
     document.querySelector("#btn").style.display = 'none';
    } else {
    document.querySelector("#btn").style.display = 'inline-block'; 
    }
    
  }
);
#btn { display: none; }
<input id="pass"> 
<button id="btn"> Show </button>

The below code should help you for the question you have asked (i.e. it will not automatically hide the button )

document.querySelector("#pass").addEventListener('input', () => {
  document.querySelector("#btn").style.display = 'inline-block';
});
#btn {
 display: none;
}
<input id="pass">
<button id="btn"> Show </button>

about 4 years ago · Juan Pablo Isaza Report

0

If I understood your question correctly, you should add a change eventlistener to input field. so that, when there is a change in input field, you can decide what to do.

input.addEventListener("change", function(){
if (input.value !== ""){
   // do something
   } else {
    do something else
   }
} )
about 4 years ago · Juan Pablo Isaza Report

0

The below snippet is the working model of what u asked 👇

please try to a avoid using innerText, innerHTML nor the on* attributes.

reference :

  • https://www.digitalocean.com/community/tutorials/js-innertext-and-innerhtml
  • Why is using onClick() in HTML a bad practice?

let input = document.getElementById('dataEntry')
let displayButton = document.getElementById('btn')

document.addEventListener('input', () => {
  if (input.value.length === 0) {
    displayButton.style.display = 'none';
  } else {
    displayButton.style.display = 'inline-block';
  }
})

displayButton.addEventListener('click', (event) => {
  if (event.target.textContent.toLowerCase() === 'hide') {
    input.type = 'password'
    event.target.textContent = 'Show'
  } else {
    input.type = 'text'
    event.target.textContent = 'Hide'
  }
})
.container {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

#btn {
  display: none;
}
<div class='container'>
  <input type='text' id='dataEntry' placeholder='Enter your password' required/>
  <button id='btn'>Hide</button>
</div>

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!