2

I am new to NodeJS, I want to use the answers from an array made in NodeJS and add them as options in a select on a HTML document. It is currently returning the error document is not defined

My HTML code is currently:

<select name="controller1" id="controller1ID">
   <option disabled selected value> -- Select Robot 1 Controller -- </option>
   <option value="sumo_example_one">Example 1</option>
   <option value="sumo_example_two">Example 2</option>
</select>

NodeJS code:

var select = document.getElementById('controller1ID');
files.forEach(function (file) {
   console.log(file); 
   var opt = document.createElement('option');
    opt.value = file;
    opt.innerHTML = file;
    select.appendChild(opt)
});
  • you haven't access to `document` object on node.js. You need to return all options you need from node.js to client and then assign them to the options via client-side js. – poltorin Aug 01 '21 at 18:07
  • NodeJS runs programs written in JavaScript, but that's where the relation to in-browser JavaScript ends, and pretty abruptly. However there absolutely is a way to do this: use [express](https://expressjs.com/en/starter/installing.html) and a view engine like [pug](https://expressjs.com/en/guide/using-template-engines.html), then pass your data to `res.render()` and use [iteration](https://pugjs.org/language/iteration.html) to create the options –  Aug 01 '21 at 18:17

0 Answers0