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

116
Visualizações
Getting JS object values from html input

So basically my issue is that I'm looking to get some data from an object. The object is picked from and html tag and the selected option is put into a function. The function handles getting the data but throws an error when trying to get the length of an array found inside the object. getRandInRange is a function I created that works perfectly so that is not the issue.

When I run the code in the browser this is the error I get

Uncaught TypeError: Cannot read properties of undefined (reading 'length')
    at buildHorde (app.js:46:60)
    at setValue (app.js:33:5)
    at HTMLInputElement.onclick (index.html:27:101)
function buildHorde(obj,num){
    // for every num add a random coin roll
    for (i = 0; i < num; i ++){
        coinArr.push(obj.coins[getRandInRange(0, obj.coins.length-1)]);
    }
    // following lines add the new arrays into the html document.
    document.getElementById("coins").innerHTML = coinArr;
    return;
}
<p>Select Party Level: <select name="Party Level" id="getObj">
    <option value=level1>level1</option>
    <option value=level2>level2</option>
</select></p>

And this is the code to set the arguments in the function

function setValue(){
    var select = document.getElementById('getObj');
    obj = select.options[select.selectedIndex].value
    num = document.getElementById('getNum').value;
    buildHorde(obj,num);
}

level1 object

let level1 = {
    coins: ["No coin", getRandInRange(1,6) * 1000 + " Copper Pieces ", getRandInRange(1,8) * 100 + " Silver Pieces ", getRandInRange(1,4) * 10 + " Platinum Pieces "],
    goods: ["nothing ", "Extravagant painting ", "ornate statue "],
    items: ["Boots of speed ", mundane.weapon[getRandInRange(0, mundane.weapon.length-1)], "Potion of fire breathing "],
}
about 4 years ago · Juan Pablo Isaza
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