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

191
Views
How to trigger add event listener of input with javascript?

I want to auto fill an input area with javascript. When i fill it with organic key press, this input's event listener trigger another list which is releated what input is. But when i say:

document.getElementById('CardNumber').value = "123"

it does not trigger anything. image

So how can i trigger this "keyup" event listener? Any suggestion is accepted.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Since you're using keyup event-listener, you can raise new Event("keyup") by using dispatchEvent from the function where you are changing the value of element.

Reference - See the documentation here and here.

const element = document.getElementById('username');

element.addEventListener("keyup", (e) => {
  console.log(e.target.value);
}, false);

function changeValue() {
  document.getElementById('username').value = '123';
  element.dispatchEvent(new Event("keyup"));
}
<input type="text" id="username" name="username" placeholder="Enter username" />

<br />
<br />
<button onclick="changeValue()">Change value inside username</button>

about 4 years ago · Juan Pablo Isaza Report

0

Maybe, you can dispatch a keyup event whenever you are setting a new value yourself

const keyupEvent = new Event('keyup')
cardNumber.dispatchEvent(keyupEvent)

// document.getElementById("CardNumber").value = "123"
const cardNumber = document.getElementById("CardNumber");
cardNumber.addEventListener("keyup", keupLister)

function keupLister(e) {
  console.log('keyup fired',e.target.value);
}

setTimeout(() => {
  cardNumber.value = "123";
  const keyupEvent = new Event('keyup')
  cardNumber.dispatchEvent(keyupEvent)
  
}, 5000)
<input id="CardNumber" placeholder="Try this input">

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!