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

189
Views
¿Por qué no puedo actualizar un elemento de entrada dentro del evento de clic?

¿Por qué el elemento de entrada no se actualiza?

 function route() { let textfield = { inputEl: document.querySelector('#textfield'), setValue: function(str) { this.inputEl.value = str; } }; textfield.inputEl.addEventListener('click', function(event) { document.body.innerHTML += '<button id="select">Select</button>'; owner(textfield); }); } function owner(controller) { document.querySelector('#select').addEventListener('click', function(event) { controller.setValue("Hello Universe!"); }); } route();

Sé cómo solucionar este problema, pero prefiero usar controller.setValue() para actualizar la entrada.

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

0

ACTUALIZADO

He actualizado el código a continuación; con suerte, esto se acerca más a lo que está tratando de lograr.

Necesitaba agregar un contenedor donde colocar el botón (en lugar de agregarlo al body ), así que agregué un contenedor como este:

 <div id="container"> <input type="text" id="textfield" /> </div>

Luego, podemos agregarle el botón como un niño, así:

 document.getElementById('container').appendChild(btn);

Ver código actualizado a continuación

 function route() { let textfield = { inputEl: document.querySelector('#textfield'), setValue: function(str) { this.inputEl.value = str; } }; textfield.inputEl.addEventListener('click', function(event) { // add the button if it's not already in there if(document.getElementById('select')){ return; } let btn = document.createElement('button'); btn.id = 'select'; btn.innerHTML = 'Select'; document.getElementById('container').appendChild(btn); owner(textfield); }); } function owner(controller) { document.getElementById('select').addEventListener('click', function(event) { controller.setValue("Hello Universe!"); }); } route();
 <div id="container"> <input type="text" id="textfield" /> </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!