Question

How do I hide a child div of an LI without retyping a function for each set? I am planning to repeat the current html many times. Current Javascript:

function $(id){
    return document.getElementById(id);
}
function year2014(){
    if ($('2014entries').style.display == 'none'){
        $('2014entries').style.display = 'block';
    }
    else {
        $('2014entries').style.display = 'none';
    }
}
function year2013(){
    if ($('2013entries').style.display == 'none'){
        $('2013entries').style.display = 'block';
    }
    else {
        $('2013entries').style.display = 'none';
    }
}

Partial HTML:

<ul>
<li id="2014" onclick="year2014()">2014</li>
    <div id="2014entries">
        <li>content</li>
        <li>content</li>
        <li>content</li>
    </div>
<li id="2013" onclick="year2013()">2013</li>
    <div id="2013entries">
        <li>content</li>
        <li>content</li>
        <li>content</li>
    </div>
</ul>
Was it helpful?

Solution

Why you don't try something like:

function year(yearValue){
    if ($(yearValue+'entries').style.display == 'none'){
        $(yearValue+'entries').style.display = 'block';
    }
    else {
        $(yearValue + 'entries').style.display = 'none';
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top