Checking the amount of elements inside a div with an id inside a class in javascript?

0

I want to access a id within a css class to check the number of paragraphs within it. In javascript how could I do it in this case?

Example:

<div class="classe">
    <div id="algo">
       <p>paragrafo1</p>
       <p>paragrafo2</p>
       <p>paragrafo3</p>
    </div>
</div>
    
asked by anonymous 22.03.2017 / 22:31

1 answer

2

You can use document.querySelectorAll to select multiple elements with a CSS selector.

In your case it could look like this:

var qtd = document.querySelectorAll('#algo p').length;
console.log('A quantidade de <p>\'s é', qtd);
<div class="classe">
    <div id="algo">
       <p>paragrafo1</p>
       <p>paragrafo2</p>
       <p>paragrafo3</p>
    </div>
</div>
    
22.03.2017 / 22:39