Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i use some div with same id like
HTML
<div id="skillDiv">
<span id="otherChk">test</span>
</div>
<div id="skillDiv">
<span id="otherChk">good</span>
</div>



Now i want to count total number of div which id is 'skillDiv' and i use this code
C#
var elements = document.all('skillDiv').length;


here when only one div is exist then it show element = undefined and if div is exist more than one then it show the right number.how to solve this problem? Plz help me.

What I have tried:

var elements = document.all('skillDiv').length;
Posted
Updated 8-Jun-16 20:28pm

Here id must be unique. It is used for identity of that element.use class instead.

in jquery

JavaScript
$(".classname").length 


will give you the count.
 
Share this answer
 
v2
Id should be unique. If you want to provide some common thing to some elements, use the class property and assign the same class to all the elements you want.
HTML
<div class="skillDiv">
<span id="otherChk">test</span>
</div>
<div class="skillDiv">
<span id="otherChk">good</span>
</div>

Now you can select elements by the common class you have provided.
 
Share this answer
 
You are doing it wrong. This is not valid HTML, because it is required that all values of all id elements should be unique in the document. To start with, you have to remove id="skillDiv" or make all values unique.

Now, document.all is not a standard API; this is legacy, non-standard one. Please see:
Document.

If you really need all elements of some tag name, all on the page (document), you can simply use document.getElementsByTagName("div").length:
Document.getElementsByTagName().

—SA
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900