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

227
Views
Is there any way to remove html element by context?

i have this following html code

<ui id="fruits">
    <li>apple</li>
    <li>orange</li>
    <li>banana</li>
    <li>lemon</li>
    <li>melon</li>
</ui>

now the question is: is there any function in jquery like $("#furits").removeElement("apple") to remove a element by context?

I tried the remove function in jquery, however it can only remove element by class,id,name etc, but i want remove a element by context?

thx.

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

0

To achieve your goal you can use a combination of :contains and remove(), like this:

$('#fruits li:contains("apple")').remove();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<ui id="fruits">
    <li>apple</li>
    <li>orange</li>
    <li>banana</li>
    <li>lemon</li>
    <li>melon</li>
</ui>

It's worth noting that :contains is both a greedy match and also case-sensitive. This means Apple would not match apple, but would match Crab Apple, for example.

If you require an exact match you could use filter() instead:

let search = 'apple';
$('#fruits li').filter((i, el) => el.textContent.trim() === search).remove();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<ui id="fruits">
    <li>apple</li>
    <li>orange</li>
    <li>banana</li>
    <li>lemon</li>
    <li>melon</li>
</ui>

about 4 years ago · Juan Pablo Isaza Report

0

Not to my knowledge. You would need to iterate through the children and compare values.

$('#fruits li').each(function(){
   if ($(this).text() === 'apple') {
       $(this).remove();
   }
});
about 4 years ago · Juan Pablo Isaza Report

0

I think this could help

$("#fruits li").remove( ":contains('apple')" );
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<ui id="fruits">
    <li>apple</li>
    <li>orange</li>
    <li>banana</li>
    <li>lemon</li>
    <li>melon</li>
</ui>

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!