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

187
Views
JavaScript Crear nueva lista desplegable, agregar opciones para seleccionar desde matriz

Estoy trabajando en un UserScript que agrega botones a una página web. Uno de los botones en los que estoy trabajando cuando se hace clic en él creará un div emergente. Estoy tratando de crear una lista desplegable dinámica Seleccionar/Opción dentro de la ventana emergente, con las opciones provenientes de una matriz. Sin embargo, sigo recibiendo errores y estaba buscando ayuda para que esto funcione. Aquí hay una versión modificada de mi código que se ocupa de la creación de la opción y selección.

 var button = document.createElement('button'), button.onclick = prompt; function prompt() { var blockingDiv = document.createElement('div'); blockingDiv.id = 'PopupBackground'; var divPopup = document.createElement('div'); divPopup.id = 'DivPopup'; var logo = document.createElement('div'); logo.id = 'Logo'; var content = document.createElement('div'); content.id = 'Content'; var dropList = document.createElement('select'); dropList.id = 'DropListSelect'; dropList.name = 'DropListSelect'; content.appendChild(DropList); var dropListOption = document.createElement('option'); dropListOption.id = 'DropListOptions'; dropListOption.name = 'DropListOptions'; dropList.appendChild(dropListOption); var options = [ { 'text': 'Select', 'value': '', 'defaultSelected': true, 'selected': true }, { 'text': 'Text 1', 'value': 'A', 'defaultSelected': false, 'selected': false }, { 'text': 'Text 2', 'value': 'B', 'defaultSelected': false, 'selected': false }, { 'text': 'Text 3', 'value': 'C', 'defaultSelected': false, 'selected': false } ]; var select = document.querySelector('#DropListSelect'); dropListOption.length = 0; for (var i = 0; i <= options.length - 1; i++) { var type = options[i]; dropListOption[i] = new Option(type.text, type.value, type.defaultSelected, type.selected) }

Cuando hago clic en el botón, Uncaught TypeError: Cannot set properties of null (setting 'length') at HTMLButtonElement.prompt .

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

0

Prueba esto:

 var select = document.querySelector('#DropListSelect'); select.innerHTML = ""; // delete all existing options for (var i = 0; i < options.length; i++) { var type = options[i]; select.appendChild(new Option(type.text, type.value, type.defaultSelected, type.selected)); }
about 4 years ago · Juan Pablo Isaza Report

0

Cambiar mi código a los siguientes trabajos ahora. ¡Gracias James por ponerme en el camino correcto!

 var button = document.createElement('button'), button.onclick = prompt; function prompt() { var blockingDiv = document.createElement('div'); blockingDiv.id = 'PopupBackground'; var divPopup = document.createElement('div'); divPopup.id = 'DivPopup'; var logo = document.createElement('div'); logo.id = 'Logo'; var content = document.createElement('div'); content.id = 'Content'; var dropList = document.createElement('select'); dropList.id = 'DropListSelect'; dropList.name = 'DropListSelect'; content.appendChild(DropList); var options = [ { 'text': 'Select', 'value': '', 'defaultSelected': true, 'selected': true }, { 'text': 'Text 1', 'value': 'A', 'defaultSelected': false, 'selected': false }, { 'text': 'Text 2', 'value': 'B', 'defaultSelected': false, 'selected': false }, { 'text': 'Text 3', 'value': 'C', 'defaultSelected': false, 'selected': false } ]; for (var i = 0; i <= options.length - 1; i++) { var dropListOption = document.createElement('option'); dropListOption.id = 'DropListOptions'; dropListOption.name = 'DropListOptions'; var type = options[i]; dropListOption.appendChild(new Option(type.text, type.value, type.defaultSelected, type.selected)); dropList.appendChild(dropListOption); }
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!