Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

186
Visualizações
JavaScript Create New Drop Down List, add Options to Select from Array

I'm working on a UserScript that adds Buttons to a webpage. One of the buttons I'm working on when clicked will create a popup div. I'm trying to create a dynamic Select/Option dropdown list within the popup, with the options coming from an Array. However, I keep getting errors and was looking for some help in getting this to work. Here's a modified version of my code that deals with the creation of the option & select.

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)
    }

When I click the button I get Uncaught TypeError: Cannot set properties of null (setting 'length') at HTMLButtonElement.prompt.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Give this a try:

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 Relatório

0

Changing my code to the following works now. Thank you James for getting me on the right track!

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda