Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Chris Ki
Chris Ki
2,187 Points

Show/Hide Elements Depending on Screen Size

Hi, I'm a bit stuck on how to show/hide a button upon re-sizing the screen.

I was creating a website and wanted to expand/collapse a particular div, and also show/hide a toggle button that will be displayed when on a smaller screen, so that a user can click on it to expand a collapsed div containing paragraphs.

So far, I've achieved how to expand/collapse a div depending on the current screen size by writing codes in JavaScript, but I can't seem to figure out how to make it work for a button. What I tried to do was using a css() method to change the button's display value to ' ' on small screen, and 'none' on bigger screen, but it's not working. I'd appreciate some help on solving this.

EDIT: I scraped the idea of using css() method, and just used show() and hide() to accomplish it. But, I decided to leave this question here instead of deleting it, 'cause I still want to know how you would go about doing it if you were to use css() method to accomplish this task, in place of a simple show() and hide().

Below is part of the html containing collapsible div and a button:

<div class="col-xs-12 col-sm-5 col-md-5 col-lg-5" id="leftside">
            <div class="noCollapse">
            <h1>Title</h1>
            <p class="roles">text for roles</p>
            <img src="">
            </div><!-- end of noCollapse -->

            <div class="showHide"><!--expand/collapse div-->
            <p class="fulldescription">paragraph 1</p>
            <p class="fulldescription">paragraph 2</p>
            <img src="">
            </div><!-- end of showHide -->

            <button type="button" class="btn btn-primary">Show More</button><!-- expand/collapse .showHide -->

        </div> <!-- end of leftside -->

Below is part of the css:

button.btn.btn-primary{
    display: none;/*value will change via javascript. setting the value to "" removes this attribute.*/
}

Finally the JavaScript:

<script> // script for expand/collapse
    var expandCollapse = function(){
        if ( $(window).width() < 768 ) {
            $(function(){
                // add a class .collapse to a div .showHide
                $('.showHide').addClass('collapse');
                // set display: "" in css for the toggle button .btn.btn-primary
                $('button.btn.btn-primary').css('display', '');// removes display property to make it visible
            });
        }
        else {
            $(function(){
                // remove a class .collapse from a div .showHide
                $('.showHide').removeClass('collapse');
                // set display: none in css for the toggle button .btn.btn-primary  
                $('button.btn.btn-primary').css('display', 'none');// hides button display on bigger screen
            });
        }
    }
    $(window).resize(expandCollapse); // calls the function when the window first loads
</script>

2 Answers

Damien Watson
Damien Watson
27,419 Points

Hi Chris,

You can easily achieve this through pure CSS, which I will tackle below. Seeing your question is concerning JavaScript, I will answer that first.

Some valid settings for display are: none, block, inline, inline-block. Instead of setting display to '', you need to use one of the others, maybe inline or block.

JavaScript

$('button.btn.btn-primary').css('display', 'block');

CSS Version

The alternative is with pure css to get screen size and manipulate css.

HTML as is

<div class="col-xs-12 col-sm-5 col-md-5 col-lg-5" id="leftside">
            <div class="noCollapse">
            <h1>Title</h1>
            <p class="roles">text for roles</p>
            <img src="">
            </div><!-- end of noCollapse -->

            <div class="showHide"><!--expand/collapse div-->
            <p class="fulldescription">paragraph 1</p>
            <p class="fulldescription">paragraph 2</p>
            <img src="">
            </div><!-- end of showHide -->

            <button type="button" class="btn btn-primary">Show More</button><!-- expand/collapse .showHide -->

 </div> <!-- end of leftside -->

CSS - no Javascript required, much simpler

.showHide { display:none; }

/* Desktop*/
@media screen and (min-width: 768px) {
  .showHide  { display: block; }
  #leftside > button { display: none; }
}
Chris Ki
Chris Ki
2,187 Points

I see! Thanks Damien,

I wasn't aware that empty string is not one of valid values for the display. And the CSS codes indeed do look much simpler than JavaScript.

Damien Watson
Damien Watson
27,419 Points

Yeh, and you don't have to check for resize, its automatic. These are called 'media queries' if you want to read up on them, all about responsive design.

You could have multiple in your css as well:

/* Mobile first - up to 768px wide */
body   { background-color: #F60; }

/* Tablet targetted size */
@media screen and (min-width:768px) and (max-width:1024px) {
  body   { background-color: #0F0 }
}

/* Desktop size */
@media screen and (min-width:1025px) {
  body   { background-color: #06F }
}
Carlos José
PLUS
Carlos José
Courses Plus Student 12,431 Points

In this case you don´t need JS but you can fiddle with Event listeners.

window.addEventListener("resize", function() {
  if() {
    //action if true
  }
  else if (already(width)) {
    //action if false
  }
});

Source