Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

224
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda