9
I wonder if there is the possibility of me counting the elements on the screen, for example a function that lists the amount of <li>
arranged on my website.
How could I do that? And how would this function work?
9
I wonder if there is the possibility of me counting the elements on the screen, for example a function that lists the amount of <li>
arranged on my website.
How could I do that? And how would this function work?
10
You can do it like this:
function contar(what){
return document.querySelectorAll(what).length;
}
For example on this page using contar('li')
gives 92. With this function you can pass a CSS selector like ul li.nome
which also works.
Example: http://jsfiddle.net/x082v700/
5
With pure javascript you can do so:
document.querySelectorAll('li').length
length
return the quantity of items present in your document.
5
You can use Jquery to do this, for example
$('li').size();
you can also put a class on li type <li class="item">
and use
$('.item').size();
so do not risk taking all the read present on the page.
4
Your best solution without doubt will be with the .length
and how it’s such a simple thing it doesn’t even need the jquery
.
I would only change one question, the selector, in some tests in the browsers the document.getElementsByTagName
is faster than document.querySelectorAll
.
getelementsbytagname vs querySelectorAll
So if you take all the elements I read and tell and your problem would indicate something like this:
var li = document.getElementsByTagName('li').length;
document.write('Quantidade de li: ' + li);
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
<li>F</li>
</ul>
<ul>
<li>A</li>
<li>B</li>
<li>C</li>
<li>D</li>
<li>E</li>
<li>F</li>
</ul>
3
This way it is possible, the script will scan all elements li
.
@Edit: Simplified form.
var elementos = document.getElementsByTagName('li');
document.writeln('Existem ' + elementos.length);
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
@Edit: Count implementation.
var elementos = document.getElementsByTagName('li');
var count = 0;
Array.prototype.forEach.call(elementos, function(arr) {
console.log(arr);
count++;
});
document.writeln('Existem ' + count);
<ul>
<li>1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
3
Ten easier, do it like this
$('li').length
ready, simple, easy and clean
Most beautiful visually though is using Jquery the version that was put as the best answer, solves the problem without using any other library
Yes, hj I avoid using jquery, 5 years later kkkkk
-1
try this with Jquery:
var i = 0;
$('li').each(function(){
i++
});
alert(i);
Even though some people hide their faces and don’t talk when they’re negative, I’ll talk. I gave -1 because this is not a good idea, considering that jQuery has its own methods for this. With size
and the priority length
.
Browser other questions tagged javascript html web-application
You are not signed in. Login or sign up in order to post.
+1 The answer is even better, because it has the function
– Wallace Maxters
It’s the best way to do it, I believe.
– Lucas Fontes Gaspareto