Welcome Guest ( Log In | Register)



 
Reply to this topicStart new topic
> Count Number Of Div's By A Class Name
sonesay
post Jan 4 2008, 12:51 AM
Post #1


|||[ n00b King ]|||
*********

Group: [HOSTED]
Posts: 676
Joined: 20-June 07
From: Auckland
Member No.: 45,102



I have a situation where I need to be able to count the number of div elements in a page and then use that number for a loop.

I've already tried this but it returns all child div elements.
CODE
ocument.getElementById("char_list").getElementsByTagName("div").length;

I need only the child nodes directly below the parent div 'char_list'.


THe structure is like this
CODE
<div id='char_list'>
<div class='char_node'></div>
<div class='char_node'></div>
<div class='char_node'></div>
</div>


The problem here is I also have nested div's inside the 'char_node' divs so the first code returns them all. I'm trying to just gather the char_node class divs and use it in my loop/function.
Go to the top of the page
 
+Quote Post
truefusion
post Jan 4 2008, 02:28 AM
Post #2


Ephesians 6:10-17
Group Icon

Group: [MODERATOR]
Posts: 1,891
Joined: 22-June 05
From: The World of Gentoo
Member No.: 8,528
T17 GFX Crew



CODE
function countElementsByClass(className, elementTag){
var count = new Array();
var elements = document.getElementsByTagName(elementTag);
for (var i = 0; i < elements.length; i++){
if (elements[i].className == className){
count[i] = "";
}
}
return count.length;
}

I put this together, i believe it'll get you what you want. Try it out and see if it is sufficient.
Go to the top of the page
 
+Quote Post
sonesay
post Jan 4 2008, 03:04 AM
Post #3


|||[ n00b King ]|||
*********

Group: [HOSTED]
Posts: 676
Joined: 20-June 07
From: Auckland
Member No.: 45,102



CODE
function countElementsByClass(className, elementTag){
var count = new Array();
var elements = document.getElementsByTagName(elementTag);
for (var i = 0; i < elements.length; i++){
if (elements[i].className == className){
count[i] = "";
alert('found one!');
}
}
return count.length;
}

count = setTimeout("countElementsByClass('char_node', 'div');", 5000);
setTimeout('alert(count);',10000);



Thanks for that code Truefusion, I'm not sure what exactly is happening but I always seem to get a return of 5 on 'count', I change the two paramters being passed to that function but it still returns 5 which is weird because I put in an alert in that loop and it does alert the correct number of tagElement + className combination.

I'm sure this code will come in handy but I think I need to rethink a new approach to my problem, I've got a tab contents style section I am trying to build for each character profile. The problem is the tab contents example I got off dynamic drive http://www.dynamicdrive.com/dynamicindex17/tabcontent.htm is only for a single collection of tabs. I need to modify it to work for multiple number of character profiles. I guess this is kinda going off topic but thats the reason I wanted to get the divs with the certain class names. I think I should post another topic on it to futher explain my problem and hopefully get some more assistance on how to go about doing it.
Go to the top of the page
 
+Quote Post
gogoily
post Jan 12 2008, 03:46 AM
Post #4


Advanced Member
*******

Group: Members
Posts: 100
Joined: 30-October 05
Member No.: 13,571



Try this one:
CODE
var count = 0;
var o = document.getElementById("char_list").getElementsByTagName("div");
for(var i=0;i<o.length;i+){
    if(o[i].className == "char_node")
        count ++;
}
alert(count);//You get your Div number here;)
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic

Collapse

> Similar Topics

Topics Topics
  1. Number Guessing Game(9)
  2. Php Unique Hit Counter(28)
  3. June 6th 2006 - The Devils Number(112)
  4. Need Help With C Program To Test If A Number Is Prime(12)
  5. Google A Number?(45)
  6. Advice On Impressing Someone In My Class(8)
  7. English Class Speech(7)
  8. My Life After High School(2)
  9. Mathematical Proof(11)
  10. Eclipse Exporting .jar Files(4)
  11. Final Spec's Of My Computer Build For My Hardware Class(11)
  12. Alright, I'm Taking Computer Programming As A Class In School.(8)
  13. Do You Ever Skip Class?(58)
  14. Php Email Validation(1)
  15. Zk(2)
  1. P.e. Class(2)
  2. Marijuana(3)
  3. History Class Project- Prohibition(0)
  4. Law And You Class - Competion(0)
  5. Make A Big Number!(8)
  6. Class Vs. Id?(7)
  7. Css Multiclassing(2)
  8. Html Div Help [resolved](1)
  9. Ti Basic: Pick A Number(1)
  10. Much Fun In Math Class(8)
  11. My Spy Suggestions For Team Fortress 2 Next Update S(3)
  12. Get Pan Number Online(0)
  13. Constant Interface Or Constant Class?(1)


 



- Lo-Fi Version Time is now: 21st August 2008 - 01:30 AM