0

The idea is to make two div appear or disappear based on the click. The CSS display style is set to none. Any help is appreciated.

   <div id="mainOval">
        <form id="btns">
            <input type="button" value="Timer" id="timerBtn" onclick="displayCont('Timer')"/>
            <input type="button" value="Countdown" id="ctDownBtn" onclick="displayCont('Countdown')"/>
        </form> 
</div>
<div id="Timer">
</div>
<div id="Countdown"> 
</div> 



<script type="text/javascript">
    function displayCont(inp)
        {
            var ele = document.getElementById(inp);
            var shown = ele.style.display;
            if (shown == 'none') 
                {
                    ele.style.display = 'block';
                }
            else if (shown == 'block')
                {
                    ele.style.display = 'none';
                }
        }
</script>
2
  • 2
    You are never changing of the attributes of the element, simply setting the value for shown. You need to actually change the display css value for something to happen. Commented Feb 6, 2014 at 21:33
  • Does your code really have spaces between the equals signs in your if() statements? Commented Feb 6, 2014 at 21:35

1 Answer 1

1

The correct code is:

if (shown == 'none') {
    ele.style.display = 'block';
}
else if (shown == 'block'){
    ele.style.display = 'none';
}

You have ot set the style of the element, not just assign a Javascript variable. And equality is ==, not = =.

Sign up to request clarification or add additional context in comments.

1 Comment

You don't have a style attribute in your elements. ele.style.display doesn't get the computed style from CSS, it just gets whatever is in the style="XXX" attribute.

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.