|
|
|
|
![]() ![]() |
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. |
|
|
|
Jan 4 2008, 02:28 AM
Post
#2
|
|
|
Ephesians 6:10-17 ![]() Group: [MODERATOR] Posts: 1,891 Joined: 22-June 05 From: The World of Gentoo Member No.: 8,528 ![]() |
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. |
|
|
|
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. |
|
|
|
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;) |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 21st August 2008 - 01:30 AM |