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

Jquery responsive

im trying to create a responsive webpage. on mobile layout I want sections to be able to collaps and open and on desktop layouts I want it to remain open. with Js I made the selected Divs slide open or close. i wrote an if statement to allow the collapsing and opening only if the screen is less than 500px. and initially when the page loads everything is fine. however when I resize the page and go from destop to mobile or from mobile to desktop, it retains the functionality of the original setup. What am I doing wrong?

$(document).ready(function (){ $("#phone, #fax").mask("999-999-9999"); // $(".collapsForm").focus().show();

//assigning window width value to var $screenSize // This will fire when document is ready: var $initialSize = $(window).width(); var $screenSize = $(window).width(); // This will fire each time the window is resized: $(window).resize(function(){ $screenSize = $(window).width(); }); /------------------end of $screenSize assignment--------------/ function defaultCollaps(){ $(".collapsForm").hide(); } function nextEvent() { if($screenSize < 500){$(".next-button").click(function(){ $(this).parents(".collapsForm").slideUp("slow"); $(this).parents(".collapsForm").next().next().slideDown("slow"); }); } }

function collapsToggle() { if($screenSize < 500){ $( ".secHeader" ).click(function() { $(this).next( ".collapsForm" ).slideToggle( "slow" ); }); } }

collapsToggle() nextEvent() });

2 Answers

the prob. is that the if statement shuold be put in the click event and then it will fire every time the event occurs

jQuery has a .resize() event handler for the browser window https://api.jquery.com/resize/

Check out this quick pen i made, resize the browser width to test it http://codepen.io/anon/pen/jBrONa

Hope that helps :thumbsup: