FFFFFF FFFFFF - 7 months ago 41
HTML Question

Making onClick event on Cascaded DIVs

this community sucks, there are great ppl, but Im tired of the other ones: ppl that upvoting just because the topic does not pleasures their own assess instead of not voting or upvoting for better relevance or even, ppl that dont mind they own butt. Im out of that thing.


Firstly you are not closing your <p> tag in the first line.

Secondly getElementsByClassName returns a collection. You can't collectively set properties unless you're using a framwork like jquery.

This would be the right code:


<div class="Year" type="button" onclick="showDiv()"><p>2016</p></div>
            <div class="Month" style="display:none;"><p>January</p></div>
            <div class="Month" style="display:none;"><p>February</p></div>
            <div class="Month" style="display:none;"><p>March</p></div>


function showDiv() {

   var elems = document.getElementsByClassName('Month');

    for(var i = 0; i != elems.length; i++)
        elems[i].style.display = "block";


See it live here: