Jump to content
xisto Community
Sign in to follow this  
sonesay

Count Number Of Div's By A Class Name

Recommended Posts

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.

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

<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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
}
}
return count.length;
}

count = setTimeout("countElementsByClass('char_node', 'div');", 5000);
setTimeout('alert(count);',10000); linenums:0'>function countElementsByClass(className, elementTag){var count = new Array();var elements = document.getElementsByTagName(elementTag);for (var i = 0; i < elements.length; i++){if (elements.className == className){count = "";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.

Share this post


Link to post
Share on other sites

Try this one:

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;)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.