More Advanced jQuery Showing/Hiding
Master Advanced jQuery Animation and DOM Traversal Techniques
What You'll Master in This Tutorial
SlideToggle Animation
Learn to create smooth animated transitions with customizable timing controls for revealing hidden content dynamically.
DOM Traversal Methods
Master jQuery's powerful sibling and parent targeting to manipulate specific elements within complex document structures.
Dynamic Image Swapping
Implement conditional logic to swap button images based on current state using jQuery attribute manipulation.
This exercise extends basic show/hide functionality by adding smooth animations and precise element targeting. You'll work with the slideToggle() method and learn advanced DOM traversal techniques.
Project Setup Process
Open Project Files
Navigate to the Show-Hide-Advanced folder and open history-news-jQuery.html in both your code editor and browser for preview.
Link jQuery Library
Add script tags for jquery-2.1.0.min.js and main.js before the closing body tag around line 91.
Initialize jQuery
Create the document ready function in main.js to ensure jQuery code executes after the DOM is fully loaded.
The slideToggle(500) method creates a smooth animation lasting 500 milliseconds (half a second). You can adjust this value to make animations faster or slower based on your design needs.
Initial Implementation Results
jQuery Targeting Approaches
| Feature | Global Selection | Specific Targeting |
|---|---|---|
| Code Example | $('.moreText') | $(this).siblings('.moreText') |
| Elements Affected | All matching elements | Related elements only |
| User Experience | Confusing behavior | Intuitive interaction |
DOM Relationship Concepts
Parent Container
The wrapping element that contains multiple child elements. In this case, the article div that holds both the button and hidden content.
Siblings
Elements that share the same parent container. The More button and moreText div are siblings within the same article container.
Image Swapping Logic
Check Current State
Use $(this).attr('src') to examine the current image source and determine whether it shows 'More' or 'Less'.
Conditional Swapping
Implement if/else logic to swap between more-button.png and less-button.png based on current state.
Update Attribute
Use $(this).attr('src', 'newimage.png') to dynamically change the image source for immediate visual feedback.
With the if/else conditional logic in place, buttons now properly toggle between 'More' and 'Less' states, providing clear visual feedback that matches the content visibility.
Key Takeaways

