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
Bethany Stinson
1,279 PointsJquery to make toggle open/closed by default depending on data present
How do I make my toggle closed by default if the table withing my div does not contain data and open by default if the table withing my div does contain data? I am using the below jquery and it works great but is open by default regardless of the presence/absence of data. Any help would be greatly appreciated.
$('#myDIV').click(function(){
$('#myDIVToggle').slideToggle('slow');
}).css({'cursor': 'pointer'});
1 Answer
Steven Parker
243,318 PointsYou could preset the state.
Outside of the click handler, you could preset the initial state by hiding the element if you table has no data. I don't know anything about how your table is stored or represented, but here's a generic example:
if (your_table_is_empty) {
$("#myDIVToggle").hide();
}