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

115
Views
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
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!