1

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>
1
  • Can you change the markup? Commented Apr 21, 2014 at 2:31

1 Answer 1

1

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';
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.