Exclude an element from a specific div

Asked

Viewed 1,429 times

0

Has the following html code:

<div id="lista">
   <p>Antonio Carlos Almeida Filhos</p>         
</div>

How could I exclude this paragraph only from this div without deleting other existing paragraphs on the page. We can use $("p").remove(),but in that case would be removing all existing paragraphs on the page.

2 answers

3


Friend, you can use the css selectors the same way in javascript. So you can select only the chosen element.

$("#lista p").remove();

Hugs.

  • I tried to find this solution, but got lost in relation to css selectors

  • 1

    Just follow the order of the html sequence itself. First select the mother div with "#list" then select the child you want to delete "p".

  • Ok, in this case also will only remove the paragraphs of the mother div "#list", even if you have other elements

  • 1

    Exactly, even if you have millions of "p" in html, you will only remove those within the #list.

  • ok so that’s right. Tks

  • @In taking advantage of the similar case, I have the following: for(i = 0; i < n_conv; i++)&#xA;{&#xA; $("#lista").append('<p class="text-dark"> nome</p>');&#xA;}&#I have this onefor that makes a append with paragrafos , I have this class text-dark that should make the name bold, but that’s not what happens. If I put it directly into html it works, but if I use append doesn’t work. Is there any way to do it that way?

  • I believe this is subject for another question friend Edson Ferrari.

  • Okay, I still need to get used to the stackoverflow, Tks

Show 3 more comments

1

You need to get first to div and then get the kids targets, something like that:

$("#lista").find("p").remove();
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="lista">
   <p>Antonio Carlos Almeida Filhos</p>       
</div>

<div id="lista2">
   <p>Temer da Silva</p>         
</div>

  • 1

    You don’t even need find. Just $("#list p"). remove(); it would already solve.

  • 1

    I know @Pra, for those who are starting is more semantic than practical, one does not need to master CSS selectors.

  • In this way $("#lista").find("p").remove(); if I have other css selectors I just remove the paragraphs inside that div?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.