We need to count how many elements present in the page. How do we do this in jQuery?
var count = $("*").length;
alert(count);
In the above code * indicates all elements present on the page and .length is the count of all elements.
Now suppose we have div element with two paragraphs tags and a span tag inside it.
<div id="area">
<p>Paragraph one</p>
<p>Paragraph two</p>
<span></span>
</div>
Now I want to count paragraphs present in the div element. Here we are not bothered about the elements outside the div element but only inside. We can do it like this.
var count = $("#area").find("p").length;
alert(count);
Now the output will be 2.
0 comments:
Post a Comment