A container div.example can have different 1st-level child elements (section, div, ul, nav, …). Quantity and type of those elements can vary.
I have to find the type (e.g. div) of the direct child that occurs the most.
What is a simple jQuery or JavaScript solution?
jQuery 1.7.1 is available, although it should work in IE < 9 (array.filter) as well.
Edit: Thank you @Jasper, @Vega and @Robin Maben 🙂
Iterate through the children using
.children()and log the number ofelement.tagNames you find:Now the
tagsobject holds the number of each type of tag that occurred as a child of the#parentelement.Here is a demo: http://jsfiddle.net/ZRjtp/ (watch your console for the object)
Then to find the tag that occurred the most you could do this:
The
most_usedobject now holds the tag used the most and how many times it was used.Here is a demo: http://jsfiddle.net/ZRjtp/1/