jQuery Logging

I’ve been having a lovely day at work, fiddling with jQuery. I started to come up with some really gnarly selector chains though, and I wondered what nodes they were actually addressing.

So, I wrote a tiny jQuery extension that logs the current jQuery selection to the firebug console.

  jQuery.fn.log = function (msg) {
      console.log("%s: %o", msg, this);
      return this;
  };

Now, I can just stuff a call to .log() in the middle of what I’m doing to see what I’m currently addressing. e.g.

  $(root).find('li.source > input:checkbox').log("sources to uncheck").removeAttr("checked");

The nice thing about logging to firebug is that each node becomes clickable in the console, so you can immediately see the context.

Comments 17

  1. Mark Fowler wrote:

    genius!

    Posted 27 Sep 2007 at 8:54 am
  2. Andrew Ingram wrote:

    Clever, it’ll come in very handy.

    Posted 28 Sep 2007 at 10:10 pm
  3. Nikola wrote:

    I’ll repeat Mark: genius!

    Posted 19 Oct 2007 at 3:26 pm
  4. Maxime wrote:

    Brilliant ! Thanks for sharing it

    Posted 19 Oct 2007 at 4:44 pm
  5. Kaloyan wrote:

    Cool!

    Posted 19 Oct 2007 at 5:53 pm
  6. André Luís wrote:

    Awesome!

    I would just add:

    if( console )

    inside the function. for cleanliness reasons…i might be testing on different browsers at the same time. ;)

    Posted 19 Oct 2007 at 6:08 pm
  7. http://www.ajaxbestiary.com wrote:

    Great Resource, Thank you for creating this.

    I agree on the if( console ) issue, I would also recommend a handy grep script to remove the .log commands from the script before production.

    Posted 19 Oct 2007 at 7:24 pm
  8. Cdx/tcodex@gmail.com wrote:

    you could always include firebug lite in your pages and then no need for unnecessary if statements – the script is cached after first time

    cool idea!

    Posted 19 Oct 2007 at 7:55 pm
  9. Leandro wrote:

    Great, thank you!

    Posted 19 Oct 2007 at 10:33 pm
  10. Ebyte.com wrote:

    Very cool! This will come in handy for sure.

    Posted 19 Oct 2007 at 11:17 pm
  11. ress wrote:

    any demo?

    Posted 20 Oct 2007 at 2:45 am
  12. ress wrote:

    any demo available?

    Posted 20 Oct 2007 at 2:46 am
  13. close protection forum wrote:

    Sounds good, a demo would help

    Posted 20 Oct 2007 at 6:19 am
  14. treo wrote:

    Looks like you need a Captcha ;)

    Posted 20 Oct 2007 at 1:45 pm
  15. JFR wrote:

    Hi,

    That is a great jQuery plugin, which is also working with Companion.JS (http://www.my-debugbar.com/wiki/CompanionJS/HomePage), so you can also log under IE !

    No info about node yet :-( but maybe in a future release.

    JFR
    http://www.debugbar.com

    Posted 20 Oct 2007 at 4:26 pm
  16. Joan Piedra wrote:

    Hello Dominic,
    I tried to improve the plugin with some conditionals, take a look at it :)

    http://pastemonkey.org/paste/471a49a0-fc54-4ca8-b8e7-5411404fdb0d

    Posted 20 Oct 2007 at 7:33 pm
  17. insic wrote:

    cool! thanks for sharing

    Posted 02 Sep 2008 at 4:08 pm

Post a Comment

Your email is never published nor shared. Required fields are marked *