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

153
Views
Chrome extension how to enter text in any selected text field

I am making a Chrome extension that generates nicknames when you press Ctrl+Shift+U, and it should automatically enter the generated name in the selected text field, if one is selected. My problem is that there are many types of text fields, the most basic being input. This is currently my function that generates the nickname, which works for input fields and textarea fields, but for nothing else.

//Nickname gets generated here
//...

var inp = document.querySelectorAll("input");
var txt = document.querySelectorAll("textarea");
    
for (var i=0;i<inp.length;i++) {
    if (inp[i] == document.activeElement) {
        inp[i].value = nick;
    }
}
    
for (var i=0;i<txt.length;i++) {
    console.log(txt[i]);
    if (txt[i] == document.activeElement) {
        txt[i].innerHTML = nick;
    }
}

Basically, what I think I would need, is some kind of function that can act like the user is typing the name, without directly changing the value of a field.

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Thanks to the answer of wOxxOm, with a bit of modification, I got it to work. This is my final code:

//Nickname gets generated here
//...

const keyboardEventInit = {bubbles:false, cancelable:false, composed:false, key:'', code:'', location:0};
const element = document.activeElement;

if (element) {

    element.dispatchEvent(new KeyboardEvent("keydown", keyboardEventInit));
    element.value = nick;
    element.dispatchEvent(new KeyboardEvent("keyup", keyboardEventInit));
    element.dispatchEvent(new Event('change', {bubbles: true}));

}
about 4 years ago · Santiago Gelvez 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!