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

198
Views
Delete some elements in JavaScript


I'd like to delete Barcelona and Madrid items from my tag select with JavaScript.

My code HTML

<select id="menu">
    <option value="Select">Select</option>
    <option value="Madrid">Madrid</option>
    <option value="Barcelona">Barcelona</option>
    <option value="Sevilla">Barcelona</option>

</select>  

My code JS

menu.textContext = " ";

I tried menu.textContext, However it doesn't work because it deletes everything.

Does someone know how I can do this?

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

0

You can use querySelectorAll to select all option tags with a specific value property:

menu.querySelectorAll('option[value="Barcelona"],option[value="Madrid"]').forEach(e => e.remove())
<select id="menu">
  <option value="Select">Select</option>
  <option value="Madrid">Madrid</option>
  <option value="Barcelona">Barcelona</option>
  <option value="Sevilla">Barcelona</option>

</select>

Alternatively, you can store the values you wish to delete in an array, select all option tags, loop through each and check whether the array includes its value. If so, delete that element:

const valuesToDelete = ['Madrid', 'Barcelona'];

menu.querySelectorAll('option').forEach(e => valuesToDelete.includes(e.value) ? e.remove() : '')
<select id="menu">
  <option value="Select">Select</option>
  <option value="Madrid">Madrid</option>
  <option value="Barcelona">Barcelona</option>
  <option value="Sevilla">Barcelona</option>

</select>

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!