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
Torger Angeltveit
11,228 PointsDifference between .append() and just .show() hidden content?
If i want to show a div with some content in it with a click function. Should i just hide the entire div and use .show(); to show it?
OR
should i use .append on every element inside that div to show it when the button is clicked?
Is there any preference and why? Load time etc?
1 Answer
jsdevtom
16,963 PointsFor the people who are unsure, these are jQuery methods not native JavaScript methods.
The difference: append() creates and then appends and element (see description below), whereas show() displays an existing element.
'The matched elements will be revealed immediately, with no animation. This is roughly equivalent to calling .css( "display", "block"), except that the display property is restored to whatever it was initially. If an element has a display value of inline, then is hidden and shown, it will once again be displayed inline.'
'The .append() method inserts the specified content as the last child of each element in the jQuery collection (To insert it as the first child, use .prepend()).'
Torger Angeltveit
11,228 PointsTorger Angeltveit
11,228 PointsBut what way is preferd? :P