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

124
Views
Document.write method

So, I'm trying to make a JS action where it should display the selected items from the list, I can only select one but I want to select more than one and view it in a list

<body>
  <label for="issue">Issue Type:</label>

  <select multiple="multiple" name="issue" id="issue">
    <option value="passport">passport</option>
    <option value="selfie">selfie</option>
    <option value="nationalId">nationalId</option>

  </select>

  <p> you are missing the following information:</p>
  <ul id="issue-type">
    <script>
      var output = '';
      let issues = document.getElementById("issue");
      for (i = 0; i < issues.length; i++) {
        output += document.write("<li>" + issues[i].text + </li>").innerHTML = issues;
      }
    </script>
  </ul>

</body>

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

0

You need to use an event listener to listen for a change of selection and you need to update the html of the element. You rarely ever want to use document.write in an application.

const issueSelect = document.getElementById("issue");
// listen for a change
issueSelect.addEventListener("change", selChanged);

function selChanged() {
  var output = '';
  let issues = issueSelect.options;
  // loop over the options
  for (var i = 0; i < issues.length; i++) {
    // is it selected?
    if (issues[i].selected) {
      // yes, build a list item
      output += "<li>" + issues[i].value + "</li>";
    }
  }
  // set the list's content
  document.getElementById("issue-type").innerHTML = output;
}
<body>
  <label for="issue">Issue Type:</label>

  <select multiple="multiple" name="issue" id="issue">
    <option value="passport">passport</option>
    <option value="selfie">selfie</option>
    <option value="nationalId">nationalId</option>

  </select>

  <p> you are missing the following information:</p>
  <ul id="issue-type">

  </ul>


</body>

How I would have coded it

const issueSelect = document.getElementById("issue");
// listen for a change
issueSelect.addEventListener("change", selChanged);

function selChanged() {
  const selectedOpts = issueSelect.querySelectorAll("option:checked");
  const output = [...selectedOpts].map(opt => `<li>${opt.value}</li>`).join('');
  document.getElementById("issue-type").innerHTML = output;
}
<body>
  <label for="issue">Issue Type:</label>

  <select multiple="multiple" name="issue" id="issue">
    <option value="passport">passport</option>
    <option value="selfie">selfie</option>
    <option value="nationalId">nationalId</option>

  </select>

  <p> you are missing the following information:</p>
  <ul id="issue-type">

  </ul>


</body>

about 4 years ago · Juan Pablo Isaza Report

0

Here is one solution and I commented each line.

let output = '';
let issues = document.querySelector("#issue");
let issue_types = document.querySelector("#issue-type");

issues.addEventListener("change", function(e) { //change event listener to check for when the select is changed
  issue_types.innerHTML = ""; //empties destination div
  let options = e.target.selectedOptions; //grabs the selected options
  options = Array.from(options).map(({ value }) => value); //converts the selected options to an array of values
  options.forEach(function(opt){ //loops through the options
    let li = document.createElement("li"); //creates a LI element
    li.innerHTML = opt; //sets the innerHTML of the list item to the option
    issue_types.appendChild(li) //appends the list to the destination UL
  });
});
<label for="issue">Issue Type:</label>

  <select multiple="multiple" name="issue" id="issue">
    <option value="passport">passport</option>
    <option value="selfie">selfie</option>
    <option value="nationalId">nationalId</option>

  </select>

  <p> you are missing the following information:</p>
  <ul id="issue-type">
  </ul>

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!