jQuery for the win

I needed to toggle some text between “show help” and “hide help”.

First, I wrote this and thought I was good:


Show HelpHide Help

That flips the text from “Show Help” to “Hide Help”. But the code can be made even shorter by calling toggle on both spans at once:


I heart jQuery.

5 thoughts on “jQuery for the win

  1. I agree completely. I love it so much, I rewrote the monitor (the 60′ plasma on my desk) in JQuery with a CherryPy backend. Again, I loves the JQuery.

    Along the same lines as your code (the whole “greedy selector thing” if you will) it has a flippin sweet method called `each`.

    Check it out:

    $('img').each(
    function() {
    $(this).attr('src',$(this).attr('src'));
    }
    );

    Iterates through every img element and basically refreshes the src attribute.

  2. Ew. Can you fix me formatting in the above? I seem to have mucked up the markdown. No preview button?!

  3. So, that “each” will go out and grab the most recent version of that image? Neato. I can see how that would be really useful for a monitor app, since the images are dynamically generated.

    You’re a perl person — what’s MTFNPY all about?

Comments are closed.