Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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(this).attr("title","Show Playlist");         
     $j(this).children("div").html('Show Playlist');
 }, function() {
     $j(this).attr("title","Hide Playlist");
     $j(this).children("div").html('Hide Playlist');
share|improve this question
up vote 5 down vote accepted

Well, it can be written differently with less repetition, but it's readable as it is. There are 4 things that need to happen when the toggling occurs; your code does exactly that. It's the simplest way to write it, really.

My only real suggestion would be to cache $j(this) in a variable to avoid recreating it.

Regardless, here's a funky rewrite, just for the sake of it

function toggle(hiding) {
  var title   = (hiding ? "Show Playlist" : "Hide Playlist"),
      symbol  = (hiding ? "&#9960"        : "&#9650"),
      method  = (hiding ? "slideUp"       : "slideDown");

  return function () {
    var element = $j(this);
    element.attr("title", title).children("div").html(title);

$j('#hideShowBtn').toggle( toggle(true), toggle(false) );
share|improve this answer
I actually really like that solution, thanks a lot! – Lelly Nov 23 '12 at 13:33
@Lelly Glad you found it useful. But do check out ZeroOne's answer too, and read its comments. There's a lot to gain by keeping the content in the markup, and only the behavior in the JS – Flambino Nov 23 '12 at 17:35

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:

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

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


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

share|improve this answer
I think this is the neater approach. In the original code there is a lot of html/content embedded in the code, so anyone reading the .html would be unable to see what is going on. This approach means the javascript is just the action. – Quango Nov 23 '12 at 9:03
I agree with @Quango - keep content and behavior separate. Other alternatives would be to use a class and some CSS so show/hide elements (and simply use .toggleClass()), or store the hide/show strings in data-* attributes so they're in the HTML and not the JS. – Flambino Nov 23 '12 at 9:13

Another option (though I have to say that I like @ZeroOne's solution in this case):

var getToggleHandler = function (opts) {
  return function () {
    var $this = $j(this);
    $this.attr("title", opts.title);

  method: 'slideUp',
  title: 'Show Playlist',
  spanContent: '&#9660;'
}), getToggleHandler({
  method: 'slideDown',
  title: 'Hide Playlist',
  spanContent: '&#9650;'
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.