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

160
Views
Problem with lists options javascript and doubleclick

I made two selections and when I press one select I want data to be transferred to the other and deleted from the original one. and vice versa at one point my value 1 is deleted and only 2 remain

Here is the code I tested Is it a problem with me or is it just a normal bug

const insertList = (list) => {
  if (list === 1) {
    let selectOne = document.getElementById("list_one");
    let selectTwo = document.getElementById("list_two");
    let option = document.createElement("option");

    console.log(selectOne.value)

    option.text = selectOne.value;
    selectTwo.add(option);
    selectOne.remove(option);

  } else if (list === 2) {
    let selectTwo = document.getElementById("list_two");
    let selectOne = document.getElementById("list_one");
    let option = document.createElement("option");

    console.log(selectTwo.value)
    option.text = selectTwo.value;
    selectOne.add(option);
    selectTwo.remove(option);
  }
}

document.getElementById('list_one').ondblclick = () => {
  insertList(1);
};

document.getElementById('list_two').ondblclick = () => {
  insertList(2);
};
<label for="list_one">List 1</label>

<select name="list_one" id="list_one" multiple size="3">
  <option value="1">1</option>
</select>
<br><br>
<label for="list_two">List 2</label>

<select name="list_two" id="list_two" multiple size="3">
  <option value="2">2</option>
</select>

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

0

  1. You are looking for textContent
  2. You can simplify the code to make it a little more DRY by delegating

document.getElementById("container").addEventListener("dblclick", function(e) { 
  const tgt = e.target.closest("label");
  if (!tgt) return; // we did not click a select
  let option = document.createElement("option");
  const listA = tgt.querySelector("select"); // clicked select
  const sibling = tgt.nextElementSibling || tgt.previousElementSibling; // the other sibling
  const listB = sibling.querySelector("select");
  option.textContent = listA.options[listA.selectedIndex].textContent; // get the text
  option.value = listA.value; // get the value
  option.selected=true; // set the option to selected to focus it when copying
  listB.add(option);
  listA.remove(option);
})
label { margin-bottom: 5px; display: block }

select { width:50px}
<div id="container">
  <label>List 1
    <select name="list_one" id="list_one" multiple size="3">
      <option value="1">One</option>
      <option value="2">Two</option>
      <option value="3">Three</option>
    </select>
  </label>
  <label>List 2
    <select name="list_two" id="list_two" multiple size="3">
      <option value="4">Four</option>
      <option value="5">Five</option>
      <option value="6">Six</option>
    </select>
  </label>
</div>

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!