HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptMinor

Revealing and hiding a playlist

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
andplaylisthidingrevealing

Problem

Is there a better way of writing this? I feel like there's a lot of lines for such a small thing. Can it be optimized?

$j('#hideShowBtn').toggle(function() {
     $j('.playlist-bar-tray').slideUp();
     $j(this).attr("title","Show Playlist");         
     $j(this).children("div").html('Show Playlist');
     $j(this).children("span").html('▼');
 }, function() {
     $j('.playlist-bar-tray').slideDown();
     $j(this).attr("title","Hide Playlist");
     $j(this).children("div").html('Hide Playlist');
     $j(this).children("span").html('▲');
});

Solution

How about "cheating" a bit by creating two copies of those elements whose values should change and then just alternate between which element to show. Something like this:


    ▼ Show playlist

    ▲ Hide playlist


And then somewhere in your JavaScript you call the .toggle() function from the jQuery UI:

$(".toggleable").toggle();


Now it would look as though the texts changed, while actually you are hiding one element and displaying another.

Code Snippets

<div class="toggleable">
    <span>&#9660;</span> Show playlist
</div>
<div class="toggleable" style="display: none;">
    <span>&#9650;</span> Hide playlist
</div>
$(".toggleable").toggle();

Context

StackExchange Code Review Q#18917, answer score: 7

Revisions (0)

No revisions yet.